Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
To bring in information, a program opens a stream on an information source (a file, memory, a socket) and reads the information sequentially, as shown in the following figure.Similarly, a program can send information to an external destination by opening a stream to a destination and writing the information out sequentially, as shown in the following figure. Reading information into a program.
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. Writing information out of a program.
The
Reading and Writing Algorithm for Data Reading Writing open a stream while more information read information close the stream open a stream while more information write information close the streamjava.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 thejava.io
package. The stream classes are divided into two class hierarchies, based on the data type (either characters or bytes) on which they operate, as shown in the following figure.The
java.io
package contains two independent hierarchies of classes: onefor reading and writing bytes and the other for reading and writing characters.
Reader
andWriter
are the abstract superclasses for character streams injava.io
.Reader
provides the API and partial implementation for readers streams that read 16-bit characters andWriter
provides the API and partial implementation for writers streams that write 16-bit characters. Subclasses ofReader
andWriter
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). The following figure shows the class hierarchies for theReader
andWriter
classes.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. The class hierarchies for readers and writers in
java.io
. Subclasses ofReader
andWriter
implement specialized streams and are divided into two categories: those that read from or write to data sinks (shaded) and those that perform some sort of processing (unshaded).
To read and write 8-bit bytes, programs should use the byte streams, descendents ofInputStream
andOutputStream
.InputStream
andOutputStream
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
andObjectOutputStream
, are used for object serialization. These classes are covered in Object Serialization.As with
Reader
andWriter
, subclasses ofInputStream
andOutputStream
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).The class hierarchies for readers and writers in
java.io
. Subclasses ofReader
andWriter
implement specialized streams and are divided into two categories: those that read from or write to data sinks (shaded) and those that perform some sort of processing (unshaded).LineNumberInputStream
is deprecated; useLineNumberReader
instead.
Reader
andInputStream
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:Also, bothint read() int read(byte cbuf[]) int read(byte cbuf[], int offset, int length)Reader
andInputStream
provide methods for marking a location in the stream, skipping input, and resetting the current position.
Writer
andOutputStream
are similarly parallel.Writer
defines these methods for writing characters and arrays of characters:Andint write(int c) int write(char cbuf[]) int write(char cbuf[], int offset, int length)OutputStream
defines the same methods but for bytes:All of the streams readers, writers, input streams, and output streams are automatically opened when created. Close a stream by calling itsint write(int c) int write(byte cbuf[]) int write(byte cbuf[], int offset, int length)close
method. A program should close a stream as soon as it is done with it, in order to free up system resources.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.