InputStreamReader class in Java
Last Updated :
21 Feb, 2022
An InputStreamReader is a bridge from byte streams to character streams. It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform’s default charset may be accepted.
Declaration :
public class InputStreamReader
extends Reader
Constructors :
- InputStreamReader(InputStream in_strm) : Creates an InputStreamReader that uses the default charset.
- InputStreamReader(InputStream in_strm, Charset cs) : creates an InputStreamReader that uses the given charset.
- InputStreamReader(InputStream in_strm, CharsetDecoder dec) : Creates an InputStreamReader that uses the given charset decoder.
- InputStreamReader(InputStream in_strm, String charsetName) : Creates an InputStreamReader that uses the named charset
Methods:
- ready() : java.io.InputStreamReader.ready() tells whether the Character stream is ready to be read or not. An InputStreamReader is ready if its input buffer is not empty, or if bytes are available to be read from the underlying byte stream.
Syntax :
public boolean ready()
Returns :
True : if the Character stream is ready to be read
False : if the Character stream is not ready to be read
- close() : java.io.InputStreamReader.close() closes InputStreamReader and releases all the Streams associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException.
Syntax :
public void close()
Returns :
No value is returned
- Implementation of ready() and close() method :
Java
import java.io.*;
public class NewClass
{
public static void main(String[] args)
{
try
{
FileInputStream geek = new FileInputStream( "ABC.txt" );
InputStreamReader in_strm = new InputStreamReader(geek);
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();
geek.close();
}
catch (FileNotFoundException fnfe)
{
System.out.println( "NO Such File Exists" );
}
catch (IOException except)
{
System.out.println( "IOException occurred" );
}
}
}
|
- Note :
All the programs in this article won’t run on online IDE as no ‘ABC’ file exists. You can check this code on Java compiler on your system.
To check this code, create a file ‘ABC’ on your system.
‘ABC’ file contains :
Geeks
For
Geeks
Output :
Character : G
Ready? : true
Character : e
Ready? : true
Character : e
Ready? : true
Character : k
Ready? : true
Character : s
Ready? : true
Character :
Ready? : true
Character :
Ready? : true
Character :
Ready? : true
Character : F
Ready? : true
Character : o
Ready? : true
Character : r
Ready? : true
Character :
Ready? : true
Character :
Ready? : true
Character :
Ready? : true
Character : G
Ready? : true
Character : e
Ready? : true
Character : e
Ready? : true
Character : k
Ready? : true
Character : s
Ready? : false
- getEncoding() : java.io.InputStreamReader.getEncoding() returns the name of the character encoding being used by this stream.
Syntax :
public String getEncoding()
Parameters :
Returns :
No value is returned
- Implementation of getEncoding() method :
Java
import java.io.*;
public class NewClass
{
public static void main(String[] args)
{
try
{
FileInputStream geek = new FileInputStream( "ABC.txt" );
InputStreamReader in_strm = new InputStreamReader(geek);
String encoding = in_strm.getEncoding();
System.out.println( "Encoding used : " +encoding);
in_strm.close();
geek.close();
}
catch (FileNotFoundException fnfe)
{
System.out.println( "NO Such File Exists" );
}
catch (IOException except)
{
System.out.println( "IOException occurred" );
}
}
}
|
Encoding used : UTF8
- read() : java.io.InputStreamReader.read() Returns single character after reading.
Syntax :
public int read()
Returns :
Returns single character after reading or -1 if the end of the stream has been reached
Java
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws FileNotFoundException, IOException
{
FileInputStream geek = new FileInputStream( "ABC.txt" );
InputStreamReader in_strm = new InputStreamReader(geek);
int t;
String read_reslt= "" ;
while ((t = in_strm.read()) != - 1 )
{
read_reslt = read_reslt+( char )t;
}
System.out.println(read_reslt);
}
}
|
- Note :
‘ABC’ file contains :
1
Geeks
2
For
3
Geeks
Output :
1
Geeks
2
For
3
Geeks
Share your thoughts in the comments
Please Login to comment...