Tóm Tắt
Java InputStreamReader
In this tutorial, we will learn about the InputStreamReader
class in Java. This class extends an abstract class Reader
. This class converts data from byte format to characters.
Constructor
All the variants of the constructor supported by InputStreamReader
class is given in the table below.
Constructor
Description
InputStreamReader(InputStream in)
This method creates an InputStreamReader that uses the default charset.
InputStreamReader(InputStream in, Charset cs)
This method creates an InputStreamReader that uses the given charset.
InputStreamReader(InputStream in, CharsetDecoder dec)
This method creates an InputStreamReader that uses the given charset decoder.
InputStreamReader(InputStream in, String charsetName)
This method creates an InputStreamReader that uses the named charset.
Method
Methods of the InputStreamReader
class is given in the table below:
Method
Description
void close()
This method closes the stream and releases any system resources associated with it.
String getEncoding()
This method returns the name of the character encoding being used by this stream.
int read()
This method reads a single character.
int read(char[] cbuf, int offset, int length)
This method reads characters into a portion of an array.
boolean ready()
This method tells whether this stream is ready to be read.
Example 1
In the following example, we are implementing InputStreamReader
to read the data from a file. Firstly we created an input stream using FileInputStream class by passing a file path in a constructor. Then using the read() method we are reading each character one by one till the stream reaches the end. It returns -1 when we reach the end of the stream.
package studytonight;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class StudyTonight
{
public static void main(String args[])
{
try {
InputStream stream = new FileInputStream("E:\\studytonight\\output.txt");
Reader reader = new InputStreamReader(stream);
int data = reader.read();
while (data != -1) {
System.out.print((char) data);
data = reader.read();
}
} catch (Exception e) {
System.out.print("Error: "+e.toString());
}
}
}
Hello Studytonight
output.txt
Hello Studytonight
Example of InputStreamReader
In this example, we are learning about the getEncoding()
method of InputStreamReader
class. This method will return the character encoding of a given InputStreamReader. I the encoding is not explicitly mentioned in that case It will return a default encoding. In this example, we set the encoding as UTF8 and we can see it in the output
package studytonight;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
public class StudyTonight
{
public static void main(String args[])
{
try{
FileInputStream file = new FileInputStream("E:\\studytonight\\output.txt");
InputStreamReader reader = new InputStreamReader(file, Charset.forName("UTF8"));
System.out.println("Character Encoding of the reader is: "+reader.getEncoding());
}
catch (Exception e) {
System.out.print("Error: "+e.toString());
}
}
}
Character Encoding of the reader is: UTF8
Conclusion
In this tutorial, we learned about the InputStreamReader
class and its methods, This class convert data from byte format to characters.