Reading Writing Text  «Prev 


Java InputStreamReader Constructors

The constructor connects a character reader to an underlying input stream:
public InputStreamReader(InputStream is)

public InputStreamReader(InputStream is, String encoding) throws UnsupportedEncodingException

The first constructor assumes the platform's default encoding scheme. The second constructor uses the specified encoding.
If that encoding does not exist, then an UnsupportedEncodingException, a subclass of IOException, is thrown.

For example, to attach an InputStreamReader to System.in with the default encoding (generally ISO Latin-1):
InputStreamReader isr = new InputStreamReader(System.in);

If you want to read a file encoded in Latin-5 (ASCII plus Turkish, as specified by ISO 8859-9), you might do this:
FileInputStream fin = new FileInputStream("symbol.txt");
InputStreamReader isr = new InputStreamReader(fin, "8859_9");

The read() methods read bytes from an underlying input stream and convert those bytes to characters according to the specified encoding:
public int read() throws IOException
public int read(char c[], int off, int length) throws IOException

The getEncoding() method returns a string containing the name of the encoding used by this reader:
public String getEncoding()

The remaining two methods just override methods from java.io.Reader but behave identically from the perspective of the programmer:
public boolean ready() throws IOException
public void close() throws IOException

InputStream Constructors

InputStreamReader(InputStream in) 
Creates an InputStreamReader that uses the default charset.
 
InputStreamReader(InputStream in, Charset cs) 
Creates an InputStreamReader that uses the given charset.
 
InputStreamReader(InputStream in, CharsetDecoder dec) 
Creates an InputStreamReader that uses the given charset decoder.
 
InputStreamReader(InputStream in, String charsetName) 
Creates an InputStreamReader that uses the named charset.