Java InputStreamReader Class with Example

An InputStreamReader is a bridge from byte streams to the character stream. It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or maybe given explicitly, or the platform's default charset may be accepted.

Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Constructors

The InputStreamReader class has 4 Constructors.

InputStreamReader(InputStream in_strm) :
Creates an InputStreamReader that uses the default charset.

InputStreamReader(InputStream in_strm, Charset cs) :
creates an InputStreamReader that uses the given charset.

InputStreamReader(InputStream in_strm, CharsetDecoder dec) :
Creates an InputStreamReader that uses the given charset decoder.

InputStreamReader(InputStream in_strm, String charsetName) :
Creates an InputStreamReader that uses the named charset

Method Details

1). getEncoding

public String getEncoding()

Returns the name of the character encoding being used by this stream. If the encoding has a historical name then that name is returned; otherwise, the encoding's canonical name is returned.

If this instance was created with the InputStreamReader(InputStream, String) constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method will return null if the stream has been closed.

import java.io.*; public class GetEncodingTest { public static void main(String[] args) { try { // FileInputStream FileInputStream fileInputStream = new FileInputStream("hello.txt"); // InputStreamReader object InputStreamReader reader = new InputStreamReader(fileInputStream); // to get the character encoding present in the stream String encoding = reader.getEncoding(); System.out.println("Encoding is : " + encoding); // Close InputStreamReader reader.close(); // Close FileInputStream fileInputStream.close(); } catch(FileNotFoundException ex) { System.out.println(ex.getMessage()); } catch(IOException ex) { System.out.println(ex.getMessage()); } } } output: Encoding is : UTF8

2). read

public int read() throws IOException

Reads a single character. It returns the character read, or -1 if the end of the stream has been reached.

3). read

public int read(char[] cbuf, int offset, int length) throws IOException

Reads characters into a portion of an array. It returns the number of characters read, or -1 if the end of the stream has been reached

4). ready

public boolean ready() throws IOException

Tells whether this stream is ready to be read. An InputStreamReader is ready if its input buffer is not empty, or if bytes are available to be read from the underlying byte stream.

5). close

public void close() throws IOException

Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.


In this article, we have seen Java InputStreamReader Class with Example.