3.2.1 Streams3.2 Streams and Files3.2 Streams and Files3.2.2 Standard Input and Output

3.2.1 Streams

Java supports many ways for a program to read data from an external source and to write data to an external destination. Source or destination can be a file, the main memory, another program, a network connection, and others. To cope with this variety of possibilities, Java introduces the abstract concept of a  stream.

A stream is an ordered sequence of bytes which flow from a source to a destination (or sink). In a program, we treat a stream either as an input stream from which we read information (i.e., the program is the destination of the stream) or as an output stream to which we write information (i.e., the program is the source of the stream). A program can deal with multiple input and output streams at the same time.  

The package java.io of the standard library provides many classes that let us define streams with particular characteristics. The classes can be organized in three ways:

  1. Input Streams versus Output Streams: some classes implement streams from which we can read data; other classes provide streams to which we can write data.
  2. Byte Streams versus Character Streams: some classes provide streams that transport raw bytes (used for binary data such as sounds or images); other classes provide streams that transport Unicode characters (used for text).  
  3. Data Streams versus Processing Streams: some classes provide streams that are directly connected to data sources or sinks to transport data from/to them; other classes provide streams that are connected to other streams to process the transported data.  

This organization is reflected in the hierarchy of the java.io classes: 


   Object
    |
    +-- InputStream
    |     |
    |     +-- ByteArrayInputStream
    |     +-- FileInputStream
    |     +-- PipedInputStream
    |     +-- ObjectInputStream
    |     +-- FilterInputStream
    |     +-- SequenceInputStream
    |           |
    |           +-- BufferedInputStream
    |           +-- DataInputStream
    |           +-- PushbackInputStream
    +-- OutputStream
    |     |
    |     +-- ByteArrayOutputStream
    |     +-- FileOutputStream
    |     +-- PipedOutputStream
    |     +-- ObjectOutputStream
    |     +-- FilterOutputStream
    |     +-- FilterOutputStream
    |           |
    |           +-- BufferedOutputStream
    |           +-- DataOutputStream
    |           +-- PrintStream
    +-- Reader
    |     |
    |     +-- CharArrayReader
    |     +-- PipedReader
    |     +-- StringReader
    |     +-- BufferedReader
    |     |     |
    |     |     +-- LineNumberReader
    |     +-- FilterReader
    |     |     |
    |     |     +-- PushBackReader 
    |     +-- InputStreamReader
    |           |
    |           +-- FileReader
    +-- Writer
          |
          +-- CharArrayWriter
          +-- PipedWriter
          +-- StringWriter
          +-- BufferedWriter
          +-- FilterWriter
          +-- OutputStreamWriter
                |
                +-- FileWriter

The first level of the hierarchy shows whether a stream is for input or output and whether it transports bytes or characters:

These classes are abstract, i.e., they cannot be instantiated but serve as frameworks for the classes at the second and third level of the hierarchy.

The second level of the hierarchy is classified according to the source/sink of a data stream respectively the kind of transformation performed by a processing stream; the third level of the hierarchy is for further specialization. The most important classes are:

Sometimes also the following classes come handy:

Typical uses of the most important classes are described in the following sections.


© Wolfgang Schreiner; February 3, 2005

3.2.1 Streams3.2 Streams and Files3.2 Streams and Files3.2.2 Standard Input and Output