22. The Package java.io

CHAPTER 22

The Package java.io

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, and all output is done through subclasses of the abstract class OutputStream. The one exception to this rule is the class RandomAccessFile, 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 passes bytes through verbatim but counts line terminators as they are read.

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.

An instance of class File 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 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 and DataOutput, that support the transfer of data other than bytes or characters, such as long integers, floating-point numbers and strings. The class DataInputStream implements DataInput; the class DataOutputStream implements DataOutput; and RandomAccessFile implements both DataInput and DataOutput.

The class StreamTokenizer 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. (Classes whose names are shown here in boldface are in package java.io; the others are in package java.lang and are shown here to clarify subclass relationships.)

Object	§20.1				
	interface DataInput	§22.1
	interface DataOutput	§22.2
	InputStream	§22.3
		FileInputStream	§22.4
		PipedInputStream	§22.5
		ByteArrayInputStream	§22.6
		StringBufferInputStream	§22.7
		SequenceInputStream	§22.8
		FilterInputStream	§22.9
			BufferedInputStream	§22.10
			DataInputStream	§22.11
			LineNumberInputStream	§22.12
			PushBackInputStream	§22.13
	StreamTokenizer	§22.14
	OutputStream	§22.15
		FileOutputStream	§22.16
		PipedOutputStream	§22.17
		ByteArrayOutputStream	§22.18
		FilterOutputStream	§22.19
			BufferedOutputStream	§22.20
			DataOutputStream	§22.21
			PrintStream	§22.22
	RandomAccessFile	§22.23
	File	§22.24
	interface FileNameFilter	§22.25
	FileDescriptor	§22.26
	Throwable	§20.22
		Exception
			IOException	§22.27
				EOFException	§22.28
				FileNotFoundException	§22.29
				InterruptedIOException	§22.30
				UTFDataFormatException	§22.31