In this section we will discuss about the InputStreamReader in Java.
java.io.InputStreamReader class takes input as a byte and then decode these bytes into the characters, for decoding this class uses a specified charset. InputStreamReader acts as a bridge between the byte reading to conversion of these bytes to characters. read() method in InputStreamReader is used to read characters from the stream. For efficiency, it is advised that the InputStreamReader should be wrapped by the BufferedReader.
Constructors of InputStreamReader
| InputStreamReader(InputStream in) | This constructor is used to create a new InputStreamReader which source is
a byte stream. This constructor uses the default charset to decode bytes into
the characters. Syntax : public InputStreamReader(InputStream in) |
| InputStreamReader(InputStream in, Charset cs) | This constructor is used to create a new InputStreamReader which source is
a byte stream. This constructor uses the specified charset to decode bytes
into the characters. Syntax : public InputStreamReader(InputStream in, Charset cs) |
| InputStreamReader(InputStream in, CharsetDecoder dec) | This constructor is used to create a new InputStreamReader which source is
a byte stream. This constructor uses the specified charset decoder to decode
bytes into the characters. Syntax : public InputStreamReader(InputStream in, CharsetDecoder dec) |
| InputStreamReader(InputStream in, String charsetName) | This constructor is used to create new InputStreamReader which source is a
byte stream. This constructor uses the specified named charset. Syntax : public InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException |
Methods in InputStreamReader
Example
A simple example is being given here which will demonstrate you about how to use the java.io.InputStreamReader to read bytes and convert them to the characters. In this example I have created a class named JavaInputStreamReaderExample.java where created an InputStream to as a data source for the InputStreamReader then created an InputStreamReader to read the bytes from source and decode them into the characters for efficiency I have wrapped the InputStreamReader within BufferedReader then read the characters from the buffer.
Source Code
JavaInputStreamReaderExample.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class JavaInputStreamReaderExample
{
public static void main(String args[])
{
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try
{
/*is = new FileInputStream("test.txt");
isr = new InputStreamReader(is);
int r;
while((r=isr.read()) != -1)
{
System.out.print((char)r);
}
*/
is = new FileInputStream("test.txt");
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
int r;
while((r=br.read()) != -1)
{
System.out.print((char)r);
}
}
catch(IOException ioe)
{
System.out.println(ioe);
}
finally
{
if(is != null)
{
try
{
is.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
if(isr != null)
{
try
{
isr.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
if(br != null)
{
try
{
br.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
}
Output
When you will execute the above example you will get the output as follows :

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Java IO InputStreamReader
Post your Comment