Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

20.4 InputStreamReader and OutputStreamWriter

The conversion streams InputStreamReader and OutputStreamWriter translate between character and byte streams using either a specified character 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 object is given a byte output stream as its destination and produces encoded 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 CharsetEncoder 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.
  • public InputStreamReader(InputStream in, CharsetDecoder c)
    • Creates an InputStreamReader to read from the given InputStream using the given character set decoder.
  • public InputStreamReader(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.
  • public OutputStreamWriter(OutputStream out)
    • Creates an OutputStreamWriter to write to the given OutputStream using the default character set encoding.
  • public OutputStreamWriter(OutputStream out, Charset c)
    • Creates an OutputStreamWriter to write to the given OutputStream using the given character set encoding.
  • public OutputStreamWriter(OutputStream out, CharsetEncoder c)
    • Creates an OutputStreamWriter to write to the given OutputStream using the given character set encoder.
  • public OutputStreamWriter(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 desirable—such as when you are converting the standard streams—so 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.

You can also use the data output stream you will learn about in Section 20.6.2 on page 539 to write characters as bytes using a specific Unicode encoding.

There is no ReaderInputStream class to translate characters to bytes, nor a WriterOutputStream class to translate bytes to characters.

  • + Share This
  • 🔖 Save To Your Account