In the Article, we will Discuss about the InputStreamReader class of java. In Java, the InputStreamReader class is used to read characters from an input stream, which can be a file, network socket, or any other data source that contains character data.
Java InputStreamReader Class:
The Java InputStreamReader Class is a subclass of the abstract class Reader and provides a bridge between byte streams and character streams. The class reads bytes from the input stream and decodes them into characters using a specified charset. The InputStreamReader class provides several constructors, which allow you to specify the input stream and the charset to use for decoding bytes into characters. Some of the commonly used constructors are:
InputStreamReader(InputStream in)creates an InputStreamReader that uses the default charset to decode bytes read from the specified input stream.
InputStreamReader(InputStream in, Charset cs)creates an InputStreamReader that uses the specified charset to decode bytes read from the specified input stream.
InputStreamReader(InputStream in, String charsetName)creates an InputStreamReader that uses the named charset to decode bytes read from the specified input stream.
// Creating an InputStream
InputStream input = new FileInputStream("file.txt");
// Creating an InputStreamReader
InputStreamReader reader = new InputStreamReader(input);
In the above example syntax, we have created an FileInputStream named input along with the InputStreamReader named reader.
Creating an InputStreamReader with specified encoding Character:
Syntax
// Creating FileInputStream
InputStream input = new FileInputStream("file.txt");
// Initializing character encoding
Charset charset = StandardCharsets.UTF_8;
// Creating InputStreamReader with character
InputStreamReader reader = new InputStreamReader(input, charset);
Here, we have used the Charset class to specify the character encoding in the file.
import java.io.*;
public class Main{
public static void main(String[] args) {
// Creating FileInputStream object
try (FileInputStream input = new FileInputStream("example.txt");
// Creating InputStreamReader object
InputStreamReader reader = new InputStreamReader(input, "UTF-8")) {
// Read function for reading the file
int data = reader.read();
while (data != -1) {
char character = (char) data;
System.out.print(character);
data = reader.read();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
In the above example, we create a FileInputStream object to read bytes from a text file named “example.txt”. We then create an InputStreamReader object that wraps the FileInputStream and uses the UTF-8 character set to decode the bytes into characters.
We use the read() method of the InputStreamReader to read one character at a time from the file, and we print each character to the console. We keep reading characters until the end of the file is reached, which is indicated by the read() method returning -1.
If an IOException occurs during the reading process, we catch it and print the stack trace for debugging purposes.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment