Java I/O Streams
Contents
See Also
Overview
To bring in information, a program opens a stream on an information source (a file, memory, a socket) and reads the information sequentially. Similarly, a program can send information to an external destination by opening a stream to a destination and writing the information out sequentially. No matter where the data is coming from or going to and no matter what its type, the algorithms for sequentially reading and writing data are basically the same:
Reading Writing open a stream while more information read information close the streamopen a stream while more information write information close the streamThe java.io package contains a collection of stream classes that support these algorithms for reading and writing. To use these classes, a program needs to import the java.io package. The stream classes are divided into two class hierarchies, based on the data type (either characters or bytes) on which they operate.
Character Streams
Reader and Writer are the abstract superclasses for character streams in java.io. Reader provides the API and partial implementation for readers--streams that read 16-bit characters--and Writer provides the API and partial implementation for writers--streams that write 16-bit characters. Subclasses of Reader and Writer implement specialized streams and are divided into two categories: those that read from or write to data sinks (shown in gray in the following figures) and those that perform some sort of processing (shown in white).
Most programs should use readers and writers to read and write textual information. The reason is that they can handle any character in the Unicode character set, whereas the byte streams are limited to ISO-Latin-1 8-bit bytes.
Byte Streams
To read and write 8-bit bytes, programs should use the byte streams descendants of InputStream and OutputStream.
InputStream and OutputStream provide the API and partial implementation for input streams (streams that read 8-bit bytes) and output streams (streams that write 8-bit bytes). These streams are typically used to read and write binary data such as images and sounds. Two of the byte stream classes, ObjectInputStream and ObjectOutputStream, are used for object serialization.
As with Reader and Writer, subclasses of InputStream and OutputStream provide specialized I/O that falls into two categories, as shown in the following class hierarchy figure: data sink streams (shaded) and processing streams (unshaded).
Understanding the I/O Superclasses
Reader and InputStream define similar APIs but for different data types. For example, Reader contains these methods for reading characters and arrays of characters:
int read() int read(char cbuf[]) int read(char cbuf[], int offset, int length)InputStream defines the same methods but for reading bytes and arrays of bytes:
int read() int read(byte cbuf[]) int read(byte cbuf[], int offset, int length)Also, both Reader and InputStream provide methods for marking a location in the stream, skipping input, and resetting the current position.
Writer and OutputStream are similarly parallel. Writer defines these methods for writing characters and arrays of characters:
int write(int c) int write(char cbuf[]) int write(char cbuf[], int offset, int length)And OutputStream defines the same methods but for bytes:
int write(int c) int write(byte cbuf[]) int write(byte cbuf[], int offset, int length)All of the streams--readers, writers, input streams, and output streams--are automatically opened when created. You can close any stream explicitly by calling its close method. Or the garbage collector can implicitly close it, which occurs when the object is no longer referenced.
Using the Streams
The following table lists java.io's streams and describes what they do. Note that many times, java.io contains character streams and byte streams that perform the same type of I/O but for different data types.
Type of I/O Streams Description Memory CharArrayReader
CharArrayWriter
ByteArrayInputStream
ByteArrayOutputStreamRead from and write to memory. Create on an existing array and then use the read and write methods to read from or write to the array. Memory StringReader
StringWriter
StringBufferInputStreamtop>StringReader reads characters from a String in memory. StringWriter writes to a String, collecting characters written to it in a StringBuffer, which can then be converted to a String.
StringBufferInputStream is similar to StringReader, except that it reads bytes from a StringBuffer.
Pipe PipedReader
PipedWriter
PipedInputStream
PipedOutputStreamImplement the input and output components of a pipe. Pipes are used to channel the output from one thread into the input of another. File FileReader
FileWriter
FileInputStream
FileOutputStreamCollectively called file streams, these streams are used to read from or write to a file on the native file system. Concatenation N/A
SequenceInputStreamConcatenates multiple input streams into one input stream. Object Serialization N/A
ObjectInputStream
ObjectOutputStreamUsed to serialize objects. Data
ConversionN/A
DataInputStream
DataOutputStreamRead or write primitive data types in a machine-independent format. Counting LineNumberReader
LineNumberInputStreamKeeps track of line numbers while reading. Peeking Ahead PushbackReader
PushbackInputStreamThese input streams each have a pushback buffer. When reading data from a stream, it is sometimes useful to peek at the next few bytes or characters in the stream to decide what to do next. Printing PrintWriter
PrintStreamContain convenient printing methods. These are the easiest streams to write to, so you will often see other writable streams wrapped in one of these. Buffering BufferedReader
BufferedWriter
BufferedInputStream
BufferedOutputStreamBuffer 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 nonbuffered streams and are often used with other streams. Filters FilterReader
FilterWriter
FilterInputStream
FilterOutputStreamThese abstract classes define the interface for filter streams, which filter data as it's being read or written. Convert InputStreamReader
OutputStreamWriterConvert byte streams to character streams, and vice-versa. InputStreamReaders read bytes from InputStreams and convert them to characters. OutputStreamWriters convert characters to bytes and write to an OutputStream. Default character encodings are stored in System.getProperty("file.encoding").
![]()