CHAPTER 22: The Package java.io |
![]() Previous |
![]() Java Language |
![]() Index |
![]() Next |
Input and output in Java is organized around the concept of streams. A stream is a sequence of items, usually 8-bit bytes, read or written over the course of time.
In the java.io package, all input is done through subclasses of the abstract class InputStream (S22.3), and all output is done through subclasses of the abstract class OutputStream (S22.15). The one exception to this rule is the class RandomAccessFile (S22.23), which handles files that allow random access and perhaps intermixed reading and writing of the file.
For an input stream, the source of data might be a file, a String, an array of bytes, or bytes written to an output stream (typically by another thread). There are also "filter input streams" that take data from another input stream and transform or augment the data before delivering it as input. For example, a LineNumberInputStream (S22.12) passes bytes through verbatim but counts line terminators as they are read. Subclasses of InputStream range from FileInputStream (S22.4) to PushBackInputStream (S22.13).
For an output stream, the sink of data might be a file, an array of bytes, or a buffer to be read as an input stream (typically by another thread). There are also "filter output streams" that transform or augment data before writing it to some other output stream. Subclasses of OutputStream range from FileOutputStream (S22.16) to PrintStream (S22.22).
An instance of class File (S22.24) represents a path name (a string) that might identify a particular file within a file system. Certain operations on the file system, such as renaming and deleting files, are done by this class rather than through streams.
An instance of class FileDescriptor (S22.26) represents an abstract indication of a particular file within a file system; such file descriptors are created internally by the Java I/O system.
There are two interfaces, DataInput (S22.1) and DataOutput (S22.2), that support the transfer of data other than bytes or characters, such as long integers, floating-point numbers and strings. The class DataInputStream (S22.11) implements DataInput; the class DataOutputStream (S22.21) implements DataOutput; and RandomAccessFile implements both DataInput and DataOutput.
The class StreamTokenizer (S22.14) provides some simple support for parsing bytes or characters from an input stream into tokens such as identifiers, numbers, and strings, optionally ignoring comments and optionally recognizing or ignoring line terminators.
The hierarchy of classes defined in package java.io is as follows. The classes Object (S20.1) and Throwable (S20.22) are in package java.lang. The description of class IOException (S22.27) is followed by descriptions of the other input and output exceptions.
Object interface DataInput interface DataOutput InputStream FileInputStream PipedInputStream ByteArrayInputStream StringBufferInputStream SequenceInputStream FilterInputStream BufferedInputStream DataInputStream LineNumberInputStream PushBackInputStream StreamTokenizer OutputStream FileOutputStream PipedOutputStream ByteArrayOutputStream FilterOutputStream BufferedOutputStream DataOutputStream PrintStream RandomAccessFile File interface FileNameFilter FileDescriptor Throwable Exception IOException EOFException FileNotFoundException InterruptedIOException UTFDataFormatException