InputStreamReader Class in Java
In Java, an InputStreamReader is a character input stream that gets its data from a stream of bytes.
It transforms a byte stream into a character stream by acting as a bridge between an incoming stream of bytes and an outgoing sequence of characters.
Explanation
A bridge connecting byte streams and character streams is an input stream reader: It uses a given charset to decode the bytes it reads into characters. It can be specified explicitly or by name, or it can accept the default charset used by the platform.
Syntax
public class InputStreamReader extends Reader
Example
public class InputStreamReader
{
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
Java Programming
Constructor
- InputStreamReader(InputStream in):
Using the built-in character encoding, this constructor turns bytes into characters by creating an InputStreamReader object.
- InputStreamReader(InputStream in, String charsetName):
Using the specified character encoding, an InputStreamReader object is created by this constructor.
- InputStreamReader(InputStream in, Charset cs):
Using the given charset, this character creates an inputStreamReader object that decodes bytes into characters.
- InputStreamReader(InputStream in, CharsetDecoder dec):
This constructor uses the given charset decoder to create an instance of an InputStreamReader object.
| Constructor | Description |
| InputStreamReader(InputStream in) | By default, the default charset is used when creating an InputStreamReader. |
| InputStreamReader(InputStream in, Charset cs) | Using the specified charset, it generates an InputStreamReader. |
| InputStreamReader(InputStream in, CharsetDecoder dec) | Using the supplied charset decoder, it generates an InputStreamReader. |
| InputStreamReader(InputStream in, String charsetName) | It uses the named charset to create an InputStreamReader. |
Methods
String getEncoding()
The character encoding name used by this stream is returned by this method.
Syntax:
public String getEncoding()
Int read()
One character is read using this method.
Syntax:
Public int read()
int read(char[ ] charBuffer, int offset, int length)
Using this technique, characters are read into a section of an array.
close()
Syntax:
Public void close()
boolean ready()
This procedure determines if the stream is prepared for reading. If the stream is ready for reading, it returns true.
Syntax:
Public boolean ready()
| Method | Type | Description |
| close() | void | Shuts off the stream and frees up any related system resources. |
| getEncoding() | String | Returns the name of the character encoding used by this stream. |
| read() | int | Reads just a single word. |
| read(char[] cbuf, int offset, int length) | int | A section of an array is read with characters. |
| ready() | boolean | Indicates if it is appropriate to read this stream. |
Example
import java.io.*;
public class NewClass
{
public static void main(String[] args)
{
try
{
FileInputStream g = new FileInputStream("ABC.txt");
InputStreamReader in_strm = new InputStreamReader(g);
int t;
while((t=in_strm.read())!= -1)
{
char r = (char)t;
System.out.println("Character : "+r);
boolean b = in_strm.ready();
System.out.println("Ready? : "+b);
}
in_strm.close();
g.close();
}
catch (FileNotFoundException fnfe)
{
System.out.println("NO Such File Exists");
}
catch (IOException except)
{
System.out.println("IOException occurred");
}
}
}
Output
Character: J
Ready?: true
Character: a
Ready?: true
Character: v
Ready?: true
Character: a
Ready?: true
Character: P
Ready?: true
Character: r
Ready?: true
Character: o
Ready?: true
Character: g
Ready?: true
Character: r
Ready?: true
Character: a
Ready?: true
Character: m
Ready?: true
Example
package javaProgram;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class InputStreamReaderExample {
public static void main(String[] args)
{
try {
FileInputStream frs = new FileInputStream("D:\\myfile.txt");
InputStreamReader inStream = new InputStreamReader(frs);
int data = inStream.read();
while (data != -1) {
System.out.print((char) data);
data = inStream.read();
}
inStream.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Output
JAVA
Conclusion
One benefit of Java is the InputStreamReader, which facilitates reading and modifying file content. It utilises every method in the InputStream subclass, which it inherited from the parent reader class. For this reason, every process in the subclass helps make any changes, as well as for reading, writing, and creating the InputStreamReader buffer.
