0% found this document useful (0 votes)
66 views

Week 4

Java.io supports a 16-bit (2-byte) Unicode character, for international character sets. Text files are often used as inputs and outputs between programs. If a file is empty or at EOF, returns null.

Uploaded by

api-218675166
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Week 4

Java.io supports a 16-bit (2-byte) Unicode character, for international character sets. Text files are often used as inputs and outputs between programs. If a file is empty or at EOF, returns null.

Uploaded by

api-218675166
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Streams, Input, and Output

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

Objects from the static class System


These are all objects residing in the static System class which all open streams ready to input or output data. System.in: The "standard" input stream System.out: The "standard" output stream System.err: The "standard" error output stream. Whereas the standard output may be a file, you can direct this toward an actual user.

Bits, Bytes, and Characters


bit
A single 1 or 0 2 choices (21)

A byte is 8 bits (7 + 1 for parity)


Traditionally, this meant a single character (such as ASCII)

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

Java file reading


FileReader
Reads one byte at a time Inherits .read() method from InputStreamReader Returns a single character as an integer If the file is empty or at EOF, returns -1

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

Operating on text files


1. Make filenames as configurable as possible 2. Read data into meaningful variables (know the format to do this) 3. Process the data if necessary 4. Write or Append data (know the format) 5. Close the files!

String Tokenizing
Use the split() on the string will create a string array. Another alternative is the StringTokenizer, but this is old

You might also like