The java.io package
is large,
but
most of the classes it contains fall into a well-structured
hierarchy. Most of the package consists of byte
streamssubclasses of InputStream or
OutputStream and character
streamssubclasses of
Reader or Writer. Each of these
stream subtypes has a specific purpose, and, despite its size,
java.io is a straightforward package to understand
and to use. In Java 1.4, the java.io package was
complemented by a "New I/O API"
defined in the java.nio package and its
subpackages. The java.nio package is totally new,
although it included some compatibility with the classes in this
package. It was designed for high-performance I/O, particularly for
use in servers and has a lower-level API than this package does. The
I/O facilities of java.io are still quite adequate
for most of the I/O required by typical client-side applications.
Before we consider the stream classes
that comprise the bulk of this package, let's
examine the important nonstream classes. File
represents a file or directory name in a system-independent way and
provides methods for listing directories, querying file attributes,
and renaming and deleting files.
FilenameFilter is an interface that defines a method
that accepts or rejects specified filenames. It is used by
File to specify what types of files should be
included in directory listings.
RandomAccessFile allows you to read from or write to
arbitrary locations of a file. Often, though, you'll
prefer sequential access to a file and should use one of the stream
classes.
InputStream and
OutputStream are abstract classes that define
methods for reading and writing
bytes.
Their subclasses allow bytes to be read from and written to a variety
of sources and sinks.
FileInputStream
and FileOutputStream
read from and write to files.
ByteArrayInputStream and
ByteArrayOutputStream read from and write to an
array of bytes in memory.
PipedInputStream reads bytes from a
PipedOutputStream, and
PipedOutputStream writes bytes to a
PipedInputStream. These classes work together to
implement a
pipe for communication
between threads.
FilterInputStream and
FilterOutputStream are special; they filter
input and output bytes. When you
create a FilterInputStream, you specify an
InputStream for it to filter. When you call the
read( ) method of a
FilterInputStream, it calls the read(
)
method of its
InputStream, processes the bytes it reads, and
returns the filtered bytes. Similarly, when you create a
FilterOutputStream, you specify an
OutputStream
to be filtered. Calling the
write( ) method of a
FilterOutputStream causes it to process your bytes
in some way and then pass those filtered bytes to the write(
) method of its OutputStream.
FilterInputStream and
FilterOutputStream do not perform any
filtering themselves; this is done by
their subclasses. BufferedInputStream and
BufferedOutputStream
are filtered streams that provide input and output
buffering and can
increase I/O efficiency.
DataInputStream
reads raw bytes from a stream and
interprets them in various binary formats. It has various methods to
read primitive Java data types in
their standard binary formats.
DataOutputStream allows you to write Java
primitive data types in binary format. The
ObjectInputStream
and
ObjectOutputStream classes are special. These
byte-stream classes are used for serializing and deserializing the
internal state of objects for storage or interprocess communication.
The byte streams just described are
complemented by an analogous set of character input and output
streams. Reader is the
superclass of all character input streams, and
Writer is the superclass of all character output
streams. Most of the Reader and
Writer streams have obvious byte-stream analogs.
BufferedReader is
a commonly used stream; it provides buffering for efficiency and also
has a readLine( ) method to read a line of
text at a time.
PrintWriter is another very common stream; its
methods allow output of a textual representation of any primitive
Java type or of any object (via the object's
toString( ) method).
Java 5.0 adds the
Closeable and Flushable
interfaces to identify types that have close(
) and
flush( ) methods. All
streams have a
close( ) method and implement the
Closeable interface. And all byte and character
output streams have a
flush( ) method and implement
Flushable.
In a related change, all
character output streams (and the
byte stream
PrintStream ) implement the (new in Java 5.0)
interface java.lang.Appendable, making them
suitable for use with the
java.util.Formatter
class. Similarly, all character input streams implement the
java.lang.Readable interface, making them suitable
for use with the java.util.Scanner class. Finally,
both PrintStream and
PrintWriter have been enhanced in two ways for
Java 5.0. Both now include
constructors for creating a stream that writes directly to a file.
And both include formatted-text output methods printf(
)
and
format( ). See
java.util.Formatter for details.