THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
Creates an InputStreamReader to read from the given InputStream
using the given character set encoding.

publicInputStreamReader(InputStream in, CharsetDecoder c)

Creates an InputStreamReader to read from the given InputStream
using the given character set decoder.

publicInputStreamReader(InputStream in, String enc)throws
UnsupportedEncodingException

Creates an InputStreamReader to read from the given InputStream
using the named character set encoding. If the named encoding is not
supported an UnsupportedEncodingException is thrown.

publicOutputStreamWriter(OutputStream out)

Creates an OutputStreamWriter to write to the given OutputStream
using the default character set encoding.

publicOutputStreamWriter(OutputStream out, Charset c)

Creates an OutputStreamWriter to write to the given OutputStream
using the given character set encoding.

publicOutputStreamWriter(OutputStream out, CharsetEncoder c)

Creates an OutputStreamWriter to write to the given OutputStream
using the given character set encoder.

publicOutputStreamWriter(OutputStream out, String enc)tHRows
UnsupportedEncodingException

Creates an OutputStreamWriter to write to the given OutputStream
using the named character set encoding. If the named encoding is not
supported an UnsupportedEncodingException is thrown.

The read methods of InputStreamReader simply read bytes from their associated InputStream and
convert them to characters using the appropriate encoding for that stream. Similarly, the write methods of
OutputStreamWriter 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 conversion
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 consistent, Unicode-savvy fashion using the local encoding. However,
if the default local encoding isn't what you need, you must use an explicit InputStreamReader or
OutputStreamWriter object. You will learn about the file related streams in more detail in Section 20.7
on page 540.

Free download pdf