Objects of a class that implementsCloseablecan be closed. It defines theclose( )method,
shown here:
void close( ) throws IOException
This method closes the invoking stream, releasing any resources that it may hold. This
interface is implemented by all of the I/O classes that open a stream that can be closed.
Objects of a class that implementsFlushablecan force buffered output to be written
to the stream to which the object is attached. It defines theflush( )method, shown here:
void flush( ) throws IOException
Flushing a stream typically causes buffered output to be physically written to the underlying
device. This interface is implemented by all of the I/O classes that write to a stream.
The Stream Classes
Java’s stream-based I/O is built upon four abstract classes:InputStream,OutputStream,
Reader, andWriter. These classes were briefly discussed in Chapter 13. They are used to
create several concrete stream subclasses. Although your programs perform their I/O
operations through concrete subclasses, the top-level classes define the basic functionality
common to all stream classes.
InputStreamandOutputStreamare designed for byte streams.ReaderandWriterare
designed for character streams. The byte stream classes and the character stream classes
form separate hierarchies. In general, you should use the character stream classes when
working with characters or strings, and use the byte stream classes when working with
bytes or other binary objects.
In the remainder of this chapter, both the byte- and character-oriented streams are
examined.
The Byte Streams
The byte stream classes provide a rich environment for handling byte-oriented I/O. A byte
stream can be used with any type of object, including binary data. This versatility makes
byte streams important to many types of programs. Since the byte stream classes are topped
byInputStreamandOutputStream, our discussion will begin with them.
InputStream
InputStreamis an abstract class that defines Java’s model of streaming byte input. It implements
theCloseableinterface. Most of the methods in this class will throw anIOExceptionon error
conditions. (The exceptions aremark( )andmarkSupported( ).) Table 19-1 shows the methods
inInputStream.
OutputStream
OutputStreamis an abstract class that defines streaming byte output. It implements the
CloseableandFlushableinterfaces. Most of the methods in this class returnvoidand throw
anIOExceptionin the case of errors. (The exceptions aremark( )andmarkSupported( ).)
Table 19-2 shows the methods inOutputStream.
562 Part II: The Java Library