The java.io package
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
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
OutputStream are abstract classes that define
methods for reading and writing
Their subclasses allow bytes to be read from and written to a variety
of sources and sinks.
read from and write to files.
ByteArrayOutputStream read from and write to an
array of bytes in memory.
PipedInputStream reads bytes from a
PipedOutputStream writes bytes to a
PipedInputStream. These classes work together to
pipe for communication
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
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.
FilterOutputStream do not perform any
filtering themselves; this is done by
their subclasses. BufferedInputStream and
are filtered streams that provide input and output
buffering and can
increase I/O efficiency.
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
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.
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(
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
In a related change, all
character output streams (and the
PrintStream ) implement the (new in Java 5.0)
interface java.lang.Appendable, making them
suitable for use with the
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(
format( ). See
java.util.Formatter for details.