Java Reference
In-Depth Information
The
InputStreamReader
class defines three varieties of
read()
method that will read one or more
bytes from the underlying stream, and return them as Unicode characters, using the default conversion
from the local character encoding. There are also two further constructors for
InputStreamReader
objects that will convert data from the stream using a charset that you specify.
Of course, it would be much more efficient if you buffered the stream using a
BufferedReader
object like this:
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
Here, we wrap an
InputStreamReader
object around
System.in
, and then buffer it using a
BufferedReader
object. This will make the input operations much more efficient.
A
CharArrayReader
object is created from an array and enables you to read data from the array as
though it is from a character input stream. A
StringReader
object class does essentially the same
thing, but with a
String
object.
Using Writers
The main subclasses of the
Writer
class are as shown below:
Writer
OutputStreamWriter
PipedWriter
For Writing a Character Stream
For Writing to a PipedReader
CharArrayWriter
BufferedWriter
For Buffering other Writers
For Writing to a char Array
PrintWriter
StringWriter
For Writing Formatted Data
For Writing to a String
FillterWriter
For Writing Filtered Streams
We will just look at a few details of the most commonly used of these classes.
The
OutputStreamWriter
class writes characters to an underlying binary stream. It also has a
subclass,
FileWriter
that writes characters to a stream encapsulating a file. Both of these are largely
superseded by the new I/O facilities that we will explore starting in the next chapter.