I/O Streams
• A stream is a sequence of bytes that flows from a
source to a destination
• In a program, we read information from an input
stream and write information to an output stream
• A program can manage multiple streams at a time
• The [Link] package contains many classes that
allow us to define various streams with specific
characteristics
I/O Stream Categories
• The classes in the I/O package divide input and
output streams into other categories
• An I/O stream is either a
– character stream, which deals with text data
– byte stream, which deals with byte data
• An I/O stream is also either a
– data stream, which acts as either a source or destination
– processing stream, which alters or manages information in
the stream
I/O class hierarchy
o class [Link]
o class [Link]
o class [Link]
o class [Link]
o class [Link]
o class [Link]
o class [Link]
o class [Link]
o class [Link]
o class [Link]
o class [Link]
o…
o class [Link]
o class [Link]
o class [Link]
o…
o class [Link]
Sources of data streams
• There are three standard I/O streams:
– standard input – defined by [Link]
– standard output – defined by [Link]
– standard error – defined by [Link]
• We use [Link] when we execute println
statements
• [Link] is declared to be a generic
InputStream reference, and therefore usually must
be mapped to a more useful stream with specific
characteristics
• FileInputStream and FileReader are classes
whose constructors open a file for reading
Processing streams
• Processing classes have constructors that take
InputSteams as input and produce InputStreams
with added functionality
• BufferedReader, and BufferedWriter allow you to
write bigger chunks of text to a stream.
– Buffering is a way of combining multiple reads or
writes into a single action. It is a good idea when
working with text.
– Examples: readLine() in BufferedReader and
newLine() in BufferedWriter
IOExceptions
• The following exception classes are defined in the
[Link] package:
CharConversionException
EOFException
FileNotFoundException
InterruptedIOException
InvalidClassException
InvalidObjectException
NotActiveException
NotSerializableException
ObjectStreamException
OptionalDataException
StreamCorruptedException
SyncFailedException
UnsupportedEncodingException
UTFDataFormatException
WriteAbortedException
Reading from a file: Listing 8.7
…
StringTokenizer tokenizer;
String line, name, file="[Link]";
…
try {
FileReader fr = new FileReader (file);
BufferedReader inFile = new BufferedReader (fr);
line = [Link]();
while (line != null) {
tokenizer = new StringTokenizer (line);
name = [Link]();
try {
units = [Link] ([Link]());
…
}
catch (NumberFormatException exception) {
[Link] ("Error in input. Line ignored:");
}
line = [Link]();
}
The Keyboard Class
• The Keyboard class was written by the authors of your
textbook to facilitate reading data from standard input
• Now we can examine the processing of the Keyboard
class in more detail
• The Keyboard class:
– declares a useful standard input stream
– handles exceptions that may be thrown
– parses input lines into separate values
– converts input stings into the expected type
– handles conversion problems
• Take a look at the code and ask questions next class