In this tutorial, we will learn about Java InputStreamReader and its strategies with the help of examples.
The InputStreamReader class of the java.io package can be used to change over data in bytes into data in characters.
It extends the abstract class Reader.
The InputStreamReader class works with other input streams. It is otherwise called a bridge between byte streams and character streams. This is on the grounds that the InputStreamReader reads bytes from the input stream as characters.
For instance, a few characters required 2 bytes to be stored in the storage. To read such data we can use the input stream reader that reads the 2 bytes together and changes over into the relating character.
In this article, you will learn-
Make an InputStreamReader
To make an InputStreamReader, we should import the java.io.InputStreamReader package first. When we import the package here is the manner by which we can make the input stream reader.
// Creates an InputStream
FileInputStream file = new FileInputStream(String path);
// Creates an InputStreamReader
InputStreamReader input = new InputStreamReader(file);
In the above example, we have created an InputStreamReader named input along with the FileInputStream named file.
Here, the data in the file are stored using some default character encoding.
However, we can specify the type of character encoding (UTF8 or UTF16) in the file as well.
// Creates an InputStreamReader specifying the character encoding
InputStreamReader input = new InputStreamReader(file, Charset cs);
Here, we have used the Charset class to specify the character encoding in the file.
Methods of InputStreamReader
The InputStreamReader class gives executions to various methods present in the Reader class.
read() Method
- read() – reads a single character from the reader
- read(char[] array) – reads the characters from the reader and stores in the specified array
- read(char[] array, int start, int length) – reads the number of characters equal to length from the reader and stores in the specified array starting from the start
For example, suppose we have a file named input.txt with the following content.
This is a line of text inside the file.
Let’s try to read this file using InputStreamReader.
import java.io.InputStreamReader;
import java.io.FileInputStream;
class Main {
public static void main(String[] args) {
// Creates an array of character
char[] array = new char[100];
try {
// Creates a FileInputStream
FileInputStream file = new FileInputStream("input.txt");
// Creates an InputStreamReader
InputStreamReader input = new InputStreamReader(file);
// Reads characters from the file
input.read(array);
System.out.println("Data in the stream:");
System.out.println(array);
// Closes the reader
input.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
Output
Data in the stream:
This is a line of text inside the file.
In the above example, we have made an input stream reader using the file input stream. The input stream reader is connected with the file input.txt.
FileInputStream file = new FileInputStream("input.txt");
InputStreamReader input = new InputStreamReader(file);
To read characters from the file, we have used the read()
method.
getEncoding() Method
The getEncoding() method can be used to get the sort of encoding that is used to store data in the input stream. For instance,
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.io.FileInputStream;
class Main {
public static void main(String[] args) {
try {
// Creates a FileInputStream
FileInputStream file = new FileInputStream("input.txt");
// Creates an InputStreamReader with default encoding
InputStreamReader input1 = new InputStreamReader(file);
// Creates an InputStreamReader specifying the encoding
InputStreamReader input2 = new InputStreamReader(file, Charset.forName("UTF8"));
// Returns the character encoding of the input stream
System.out.println("Character encoding of input1: " + input1.getEncoding());
System.out.println("Character encoding of input2: " + input2.getEncoding());
// Closes the reader
input1.close();
input2.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
Output
The character encoding of input1: Cp1252
The character encoding of input2: UTF8
In the above example, we have made 2 input stream reader named input1 and input2.
- input1 doesn’t determine the character encoding. Subsequently the getEncoding() technique returns the canonical name of the default character encoding.
- input2 indicates the character encoding, UTF8. Henceforth the getEncoding() technique returns the predetermined character encoding.
Note: We have used the Charset.forName() method to specify the type of character encoding. To learn more, visit Java Charset (official Java documentation).
close() Method
To close the input stream reader, we can use the close() technique. When the close() technique is called, we can’t use the reader to read the data.
Other Methods of InputStreamReader
Method | Description |
ready() | checks if the stream is ready to be read |
mark() | mark the position in stream up to which data has been read |
reset() | returns the control to the point in the stream where the mark was set |
To learn more, visit Java InputStreamReader (official Java documentation).
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.