Previous | Next | Trail Map | Writing Java Programs | Input and Output Streams


Overview of java.io's Input and Output Streams

The java.io package contains two classes, InputStream and OutputStream, from which most of the other classes in the package derive.

The InputStream class is an abstract base class that provides a minimal programming interface and a partial implementation of input streams in Java. The InputStream class defines methods for reading bytes or arrays of bytes, marking locations in the stream, skipping bytes of input, finding out the number of bytes that are available for reading, and resetting the current position within the stream. An input stream is automatically opened when you create it. You can explicitly close a stream with the close() method, or let it be closed implicitly when the object is garbage collected.

The OutputStream class is an abstract base class that provides a minimal programming interface and a partial implementation of output streams in Java. OutputStream defines methods for writing bytes or arrays of bytes to the stream and flushing the stream. An output stream is automatically opened when you create it. You can explicitly close an output stream with the close() method, or let it be closed implicitly when the object is garbage collected.

The java.io package contains several subclasses of InputStream and OutputStream that implement a specific input or output function. For example, FileInputStream and FileOutputStream are input and output streams that operate on a file on the native file system.

The first of the following two diagrams shows the class hierarchy for the input stream classes comprising the java.io package. The second diagram shows the class hierarchy for the output stream classes contained in the java.io package.


Note: you can click on any of the class symbols in either diagram to visit the API documentation for that class.



As you can see from the diagram InputStream inherits from the Object class; six classes inherit directly from InputStream. One of OutputStream's descendents, FilterInputStream, is itself an abstract class with four children.



As you can see OutputStream inherits from the Object class; four classes inherit directly from OutputStream. One of OutputStream's descendents, FilterOutputStream, is itself an abstract class with three descendents.

Simple Input and Output Streams

The following is an overview of the input and output stream classes that inherit directly from InputStream and OutputStream that are not themselves abstract classes.
FileInputStream and FileOutputStream
Reads data from or writes data to a file on the native file system.
PipedInputStream and PipedOutputStream
Implements the input and output components of a pipe. Pipes are used to channel the output from one program (or thread or code block) into the input of another. All PipedInputStreams must be connected to a PipedOutputStream and all PipedOutputStreams must be connected to a PipedInputStream.
ByteArrayInputStream and ByteArrayOutputStream
Reads data from or writes data to a byte array in memory.
SequenceInputStream
Concatenates multiple input streams into one input stream.
StringBufferInputStream
Allows programs to read from a StringBuffer as if it were an input stream.
Using Input and Output Streams later in this lesson, covers these streams.

Filtered Streams

FilterInputStream and FilterOutputStream are subclasses of InputStream and OutputStream, respectively, and are both themselves abstract classes. These classes define the interface for filtered streams. Filtered streams process data as its being read or written. For example, the BufferedInputStream and BufferedOutputStream classes buffer the data while reading and writing to speed it up.
DataInputStream and DataOutputStream
Reads or writes primitive Java data types in a machine independent format.
BufferedInputStream and BufferedOutputStream
Buffers data while reading or writing thereby reducing the number of accesses required on the original data source. Buffered streams are typically more efficient than similar non-buffered streams.
LineNumberInputStream
An input stream that keeps track of line numbers while reading.
PushbackInputStream
An input stream with a one-byte pushback buffer. Sometimes when reading data from a stream it is useful to peek at the next character in the stream in order to decide what to do next. If you peek at a character in the stream, you'll need to put it back so that it can be read again and processed normally.
PrintStream
An output stream with convenient printing methods.
The Working with Filtered Streams page later in this lesson show you how to use filtered streams and how to implement your own.

And The Rest...

In addition to the streams classes, java.io contains these other classes:
File
Represents a file on the native file system. You can create a File object for a file on the native file system and then query the object for information about that file.
FileDescriptor
Represents a file handle (or descriptor) on the native file system. [PENDING: write more about FileDescripter. Need to differentiate this from file. This class will not be covered in any more detail in this lesson.]
RandomAccessFile
Represents a random access file.
StreamTokenizer
Breaks the contents of a stream into tokens. Tokens are the smallest unit recognized by a text-parsing algorithm (such as words, symbols, and so on). A StreamTokenizer object can be used to parse any text file. For example, you could use it to parse a Java source file into variable names operators and so on, or an HTML file into HTML tags, words and such.
And interfaces:
DataInput and DataOutput
These two interfaces describe streams that can read and write primitive Java types in machine independent format. DataInputStream, DataOutputStream and RandomAccessFile implement these interfaces.
FilenameFilter
The list() method in the File class uses a FilenameFilter to determine which files in a directory to list. The FilenameFilter accepts or rejects files based on their names. You could use FilenameFilter to implement simple regular expression style file search patterns such as foo.* and so on.
Using Random Access Files talks about how to use random access files. It also provides a special section that shows you how to write filters for objects that implement the DataInput and DataOutput interfaces. Filters implemented in this fashion are more flexible than regular filtered streams because they can be used on random access files and on some sequential files.


Previous | Next | Trail Map | Writing Java Programs | Input and Output Streams