Java Reference
In-Depth Information
You can read an input stream as a character stream using an InputStreamReader object that you could
create like this:
InputStreamReader keyboard = new InputStreamReader(System.in);
The parameter to the InputStreamReader constructor is of type InputStream , so you can pass an object
of any class derived from InputStream to it. The preceding example creates an InputStreamReader ob-
ject, keyboard , from the object System.in , the keyboard input stream.
The InputStreamReader class defines the abstract read() method that it inherits from Reader and re-
defines the read() method without parameters. These methods read bytes from the underlying stream and
return them as Unicode characters using the default conversion from the local character encoding. In ad-
dition to the preceding example, there are also three further constructors for InputStreamReader objects
(shown in Table 8-5 ) .
TABLE 8-5 : InputStreamReader Object Constructors
METHOD
DESCRIPTION
Constructs an object with in as the underlying stream. The object uses s to convert bytes to
Unicode characters.
InputStreamReader(
InputStream in,
Charset s)
Constructs an object that uses the charset decoder dec to transform bytes that are read from the
stream in to a sequence of Unicode characters.
InputStreamReader(
InputStream in,
CharsetDecoder
dec)
Constructs an object that uses the charset identified in the name charsetName to convert bytes
that are read from the stream in to a sequence of Unicode characters.
InputStreamReader(
InputStream in,
String char-
setName)
A java.nio.charset.Charset object defines a mapping between Unicode characters and bytes. A
Charset can be identified by a name that is a string that conforms to the IANA conventions for Charset
registration. A java.nio.charset.CharsetDecoder object converts a sequence of bytes in a given charset
to bytes. Consult the class documentation in the JDK for the Charset and CharsetDecoder classes for more
information.
 
 
Search WWH ::




Custom Search