Sei sulla pagina 1di 8

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 java.io 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 java.lang.Object
o class java.io.InputStream
o class java.io.ByteArrayInputStream
o class java.io.FileInputStream
o class java.io.FilterInputStream
o class java.io.OutputStream
o class java.io.ByteArrayOutputStream
o class java.io.FileOutputStream
o class java.io.FilterOutputStream
o class java.io.Reader
o class java.io.BufferedReader
o…
o class java.io.InputStreamReader
o class java.io.Writer
o class java.io.BufferedWriter
o…
o class java.io.OutputStreamWriter
Sources of data streams
• There are three standard I/O streams:
– standard input – defined by System.in
– standard output – defined by System.out
– standard error – defined by System.err
• We use System.out when we execute println
statements
• System.in 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
java.io 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="inventory.dat";

try {
FileReader fr = new FileReader (file);
BufferedReader inFile = new BufferedReader (fr);
line = inFile.readLine();
while (line != null) {
tokenizer = new StringTokenizer (line);
name = tokenizer.nextToken();
try {
units = Integer.parseInt (tokenizer.nextToken());

}
catch (NumberFormatException exception) {
System.out.println ("Error in input. Line ignored:");
}
line = inFile.readLine();
}
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

Potrebbero piacerti anche