Java Reference
In-Depth Information
Stream Readers and Writers
Stream readers
and
writers
are objects that can read and write byte streams as character streams. So a
character stream is essentially a byte stream fronted by a reader or a writer. The base classes for stream
readers and writers are:
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. The
Reader
and
Writer
classes and their subclasses
are not really streams themselves, but provide the methods you can use for reading and writing binary
streams as character streams. Thus, a
Reader
or
Writer
object is typically created using an underlying
InputStream
or
OutputStream
object that encapsulates the connection to the external device,
which is the ultimate source or destination of the data.
Using Readers
The
Reader
class has the direct subclasses shown in the diagram below:
Reader
InputStreamReader
PipedReader
For Reading a Character Stream
For Reading from a PipedWriter
BufferedReader
CharArrayReader
For Buffering other Readers
For Reading from a Char Array
StringReader
FilterReader
For Reading Filtered Streams
For Reading from a String
The concrete class that you would use to read a binary input stream as a character stream is
InputStreamReader
. You could create an
InputStreamReader
object like this:
InputStreamReader keyboard = new InputStreamReader(System.in);
The parameter to the
InputStreamReader
constructor is of type
InputStream
, so you can pass an
object of any class derived from
InputStream
to it. The sample above creates an
InputStreamReader
object,
keyboard
, from the object
System.in
, the keyboard input stream.