Week 4
Week 4
Streams
A stream of bytes For a text-oriented stream, bytes equate to characters. Input: from keyboard, file, other program Output: to monitor, file, printer, other program
Java supports a 16-bit (2-byte) Unicode character, for international character sets
Buffers
A buffer is readily-available data stored in a temporary area Chunks of data are read into a buffer then processed in chunks, instead of reading and processing one byte (or character) at a time
BufferedReader
Reads a buffer of bytes readLine() method returns a string If the file is empty or at EOF, returns null
java.io exceptions
java.io.Exception
Can be thrown when the system is trying to read from or write to a file
java.io.FileNotFouundException
Thrown when attempting to open a file that does not exist
java.io.EOFException
Thrown when attempting a read command at the end of the file (can be moot if coded properly)
Text Files
Text files are often used as inputs and outputs between programs. Know the format
Consider the structure (fixed-length vs. delimited, etc) EOF Consider record totals to ensure file is complete
String Tokenizing
Use the split() on the string will create a string array. Another alternative is the StringTokenizer, but this is old