Java Reference
In-Depth Information
You investigate the methods belonging to the
ObjectOutputStream
class in Chapter 12, when you learn
how to write objects to a file.
NOTE
Note that this is not the complete set of output stream classes. The
FilterOut-
putStream
class has a further eight subclasses, including the
BufferedOutputStream
class,
which does for output streams what the
BufferedInputStream
class does for input streams.
There is also the
PrintStream
class, which you look at a little later in this chapter, because
output to the command line is via a stream object of this type.
Stream Readers and Writers
Stream readers
and
writers
are objects that can read and write byte streams as character streams. So a char-
acter stream is essentially a byte stream fronted by a reader or a writer. The base classes for stream readers
and writers are shown in
Table 8-3
.
CLASS DESCRIPTION
Reader
The base class for reading a character stream
Writer
The base class for writing a character stream
Reader
and
Writer
are both abstract classes. Both classes implement the
AutoCloseable
interface,
which declares the
close()
method. The
Reader
class also implements the
Readable
interface, which de-
clares the
read()
method for reading characters into a
CharBuffer
object that is passed as the argument to
the method. The
Reader
class defines two further
read()
methods. One of these requires no arguments and
reads and returns a single character from the stream and returns it as type
int
. The other expects an array
of type
char[]
as the argument and reads characters into the array that is passed to the method. The method
returns the number of characters that were read or −1 if the end of the stream is reached. The reader has an
abstract
read()
method as a member, which is declared like this: