Inputstreamreader

An InputStreamReader serves as a connection between byte streams and character streams. It functions by reading bytes and converting them into characters based on a designated charset. The charset can be indicated by its name, explicitly provided, or the default charset of the platform can be utilized.

Constructor

Constructor name Description
InputStreamReader(InputStream in) It creates an InputStreamReader that uses the default charset.
InputStreamReader(InputStream in, Charset cs) It creates an InputStreamReader that uses the given charset.
InputStreamReader(InputStream in, CharsetDecoder dec) It creates an InputStreamReader that uses the given charset decoder.
InputStreamReader(InputStream in, String charsetName) It creates an InputStreamReader that uses the named charset.

Method

Modifier and Type Method Description
void close() It closes the stream and releases any system resources associated with it.
String getEncoding() It returns the name of the character encoding being used by this stream.
int read() It reads a single character.
int read(char[] cbuf, int offset, int length) It reads characters into a portion of anarray.
boolean ready() It tells whether this stream is ready to be read.

Example

Example

public class InputStreamReaderExample {
	public static void main(String[] args) {
		try  {
			InputStream stream = new FileInputStream("file.txt");
			Reader reader = new InputStreamReader(stream);
			int data = reader.read();
			while (data != -1) {
				System.out.print((char) data);
				data = reader.read();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Output:

Output

I love my country

The file.txt contains text "I love my country" the InputStreamReader 
reads Character by character from the file

Input Required

This code uses input(). Please provide values below: