0% found this document useful (0 votes)
132 views8 pages

I/O Streams

Streams allow data to flow between sources and destinations in a program. There are two categories of I/O streams: character streams for text data and byte streams for binary data. Streams can also be data streams that act as sources or destinations, or processing streams that alter or manage data in the stream. The Java I/O package defines a hierarchy of classes for different stream types including input, output, byte arrays, files and filters. Programs use standard input/output streams to read from keyboard and write to screen while file streams read and write to files. Processing streams like BufferedReader add functionality like buffering reads.

Uploaded by

John Berkmans
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views8 pages

I/O Streams

Streams allow data to flow between sources and destinations in a program. There are two categories of I/O streams: character streams for text data and byte streams for binary data. Streams can also be data streams that act as sources or destinations, or processing streams that alter or manage data in the stream. The Java I/O package defines a hierarchy of classes for different stream types including input, output, byte arrays, files and filters. Programs use standard input/output streams to read from keyboard and write to screen while file streams read and write to files. Processing streams like BufferedReader add functionality like buffering reads.

Uploaded by

John Berkmans
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like