Java Reference
In-Depth Information
20.4.
InputStreamReader
and
OutputStreamWriter
The conversion streams
InputStreamReader
and
OutputStreamWriter
TRans-
late between character and byte streams using either a specified char-
acter set encoding or the default encoding for the local system. These
classes are the "glue" that lets you use existing 8-bit character encodings
for local character sets in a consistent, platform-independent fashion. An
InputStreamReader
object is given a byte input stream as its source and
produces the corresponding
UTF
-16 characters. An
OutputStreamWriter
ob-
ject is given a byte output stream as its destination and produces en-
coded byte forms of the
UTF
-16 characters written on it. For example,
the following code would read bytes encoded under
ISO
8859-6 for Arabic
characters, translating them into the appropriate
UTF
-16 characters:
public Reader readArabic(String file) throws IOException {
InputStream fileIn = new FileInputStream(file);
return new InputStreamReader(fileIn, "iso-8859-6");
}
By default, these conversion streams will work in the platform's default
character set encoding, but other encodings can be specified. Encoding
values were discussed in "
Character Set Encoding
" on page
320
;
they
can be represented by name or a
Charset
, or by a
CharsetDecoder
or
Char-
setEncoder
object from the
java.nio.charset
package.
public
InputStreamReader(InputStream in)
Creates an
InputStreamReader
to read from the given
InputStream
using the default character set encoding.
public
InputStreamReader(InputStream in, Charset c)
Creates an
InputStreamReader
to read from the given
InputStream
using the given character set encoding.