Previous | Next | Trail Map | The Java Development Environment | Package Tour


Input Streams

The following diagram shows the class hierarchy for the input stream classes comprising the java.io package. InputStream inherits from the Object class; six classes inherit directly from InputStream; FilterInputStream is an abstract class with four children.
Note: you can click on any of the class symbols in the following image to visit the API documentation for that class.

Each of the InputStreams provided by the java.io package is intended for a different purpose, either reading from a different type of input source or reading a specific type or form of data.

Simple Input Streams

To concatenate multiple input streams: use SequenceInputStream.
To read from a pipe: use PipedInputStream. Typically attached to a PipedOutputStream.
To read from a file: use FileInputStream.
To read from a buffer: use ByteArrayStreamBuffer.
To read from a StringBuffer: use StringBufferInputStream.

Filter Input Streams

FilterInputStream is an abstract class that defines the interface for filtered streams. Filtered streams have some intelligence built into them about the data that they are reading. For example, the DataInputStream class, a subclass of FilterInputStream, reads a stream of primitive Java data types. For example, suppose your input source contained tab delimited data for HotJava merchandise. The first column contains the price of the item, the second column contains the shipping weight in ounces, and the final column contains a description of the item.
19.99   12      HotJava T-shirt   
9.99    8       HotJava Mug
You would use FilterInputStream's specialized read() methods to read the input data into Java variables of the correct type. This code snippet would read a line of the above data:
float price;
int weight;
String desc;

price = myDataInputStream.readFloat();
    myDataInputStream.readChar();	// throw out the tab
weight = myDataInputStream.readInt();
    myDataInputStream.readChar();	// throw out the tab
desc = myDataInputStream.readLine();

How to Use the Filtered Streams

To read primitive Java data types: use DataInputStream.
To [PENDING: WHAT?]: use LineNumberInputStream.
For a more efficient stream that buffers while reading: use BufferedInputStream.
For a stream with a one-byte pushback buffer: use PushbackInputStream.


Previous | Next | Trail Map | The Java Development Environment | Package Tour