
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
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).