Java Reference
In-Depth Information
The
read
methods of
InputStreamReader
simply read bytes from their as-
sociated
InputStream
and convert them to characters using the appro-
priate encoding for that stream. Similarly, the
write
methods of
Out-
putStreamWriter
take the supplied characters, convert them to bytes with
the appropriate encoding, and write them to the associated
OutputStream
.
In both classes, closing the conversion stream also closes the associated
byte stream. This may not always be desirablesuch as when you are
converting the standard streamsso consider carefully when closing con-
version streams.
Both classes also support the method
getEncoding
, which returns a string
representing either the historical or canonical name of the stream's
character encoding, or
null
if the stream has been closed.
The
FileReader
and
FileWriter
classes are subclasses of these conversion
streams. This helps you read and write local files correctly in a consist-
ent, Unicode-savvy fashion using the local encoding. However, if the de-
fault local encoding isn't what you need, you must use an explicit
In-
putStreamReader
or
OutputStreamWriter
object. You will learn about the file
related streams in more detail in
Section 20.7
on page
540
.
You can also use the data output stream you will learn about in
Section
encoding.
There is no
ReaderInputStream
class to translate characters to bytes, nor
a
WriterOutputStream
class to translate bytes to characters.