img
Objects of a class that implements Closeable can be closed. It defines the close( ) 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 implements Flushable can force buffered output to be written
to the stream to which the object is attached. It defines the flush( ) 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, and Writer. 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.
InputStream and OutputStream are designed for byte streams. Reader and Writer are
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
by InputStream and OutputStream, our discussion will begin with them.
InputStream
InputStream is an abstract class that defines Java's model of streaming byte input. It implements
the Closeable interface. Most of the methods in this class will throw an IOException on error
conditions. (The exceptions are mark( ) and markSupported( ).) Table 19-1 shows the methods
in InputStream.
OutputStream
OutputStream is an abstract class that defines streaming byte output. It implements the
Closeable and Flushable interfaces. Most of the methods in this class return void and throw
an IOException in the case of errors. (The exceptions are mark( ) and markSupported( ).)
Table 19-2 shows the methods in OutputStream.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home