Java - InputStreamReader Class
Introduction
The Java InputStreamReader class is a bridge from byte streams to character streams.It reads bytes and decodes them into characters using a specified charset.
Class declaration
Following is the declaration for Java.io.InputStreamReader class −
public class InputStreamReader extends Reader
Field
Following are the fields for Java.io.InputStreamReader class −
protected Object lock − This is the object used to synchronize operations on this stream.
Class constructors
| Sr.No. | Constructor & Description |
|---|---|
| 1 | InputStreamReader(InputStream in) This creates an InputStreamReader that uses the default charset. |
| 2 | InputStreamReader(InputStream in, Charset cs) This creates an InputStreamReader that uses the given charset. |
| 3 | InputStreamReader(InputStream in, CharsetDecoder dec) This creates an InputStreamReader that uses the given charset decoder. |
| 4 | InputStreamReader(InputStream in, String charsetName) This creates an InputStreamReader that uses the named charset. |
Class methods
| Sr.No. | Method & Description |
|---|---|
| 1 | void close()
This method closes the stream and releases any system resources associated with it. |
| 2 | String getEncoding()
This method returns the name of the character encoding being used by this stream. |
| 3 | int read()
This method reads a single character. |
| 4 | int read(char[] cbuf, int offset, int length)
This method reads characters into a portion of an array. |
| 5 | boolean ready()
This method tells whether this stream is ready to be read. |
Methods inherited
This class inherits methods from the following classes −
- Java.io.Reader
- Java.io.Object
Example - Using close() with InputStreamReader (Automatic Closure with Try-With-Resources)
The following example shows the usage of Java InputStreamReader close() method.
InputStreamReaderDemo.java
package com.tutorialspoint;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderDemo {
public static void main(String[] args) {
try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt"))) {
int data;
while ((data = reader.read()) != -1) { // Read character by character
System.out.print((char) data);
}
// No need to manually close, try-with-resources handles it
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output(if example.txt contains "JavaProgramming")
Let us compile and run the above program, this will produce the following result−
JavaProgramming
Explanation
Uses InputStreamReader, which reads characters from a file.
Reads one character at a time using read().
Uses try-with-resources, which automatically calls close() at the end.
Example - Getting Default Encoding of InputStreamReader
The following example shows the usage of Java InputStreamReader getEncoding() method.
InputStreamReaderDemo.java
package com.tutorialspoint;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderDemo {
public static void main(String[] args) {
try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt"))) {
System.out.println("Encoding used: " + reader.getEncoding());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
Let us compile and run the above program, this will produce the following result−
Encoding used: UTF8
Explanation
Creates an InputStreamReader with default encoding.
Calls getEncoding() to print the encoding used.
The encoding depends on the JVM default (e.g., "UTF8" or "Cp1252" on Windows).
Example - Reading One Character at a Time
The following example shows the usage of Java InputStreamReader read() method.
InputStreamReaderDemo.java
package com.tutorialspoint;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderDemo {
public static void main(String[] args) {
try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt"))) {
int data;
while ((data = reader.read()) != -1) { // Read character by character
System.out.print((char) data); // Convert integer to character and print
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output(if example.txt contains "Hello")
Let us compile and run the above program, this will produce the following result−
Hello
Explanation
Uses InputStreamReader to read text from "example.txt".
Calls read() to read one character at a time.
Converts the integer Unicode value into a character ((char) data).
Stops reading when read() returns -1 (EOF reached).