Open In App

InputStreamReader class in Java

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 : 

Methods: 



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
public void close()
Returns :
No value is returned




// Java program illustrating ready() and close() method
 
import java.io.*;
public class NewClass
{
    public static void main(String[] args)
    {
 
        try
        {
            // initializing FileInputStream
            FileInputStream geek = new FileInputStream("ABC.txt");
 
            // Initializing InputStreamReader object
            InputStreamReader in_strm = new InputStreamReader(geek);
 
            int t;
            while((t=in_strm.read())!= -1)
            {
                // convert the integer true to character
                char r = (char)t;
                System.out.println("Character : "+r);
 
                // check if the stream in_strm ready
                boolean b = in_strm.ready();
                // Use of ready() methods
                System.out.println("Ready? : "+b);
 
            }
             
            // Use of close() method to Close InputStreamReader
            in_strm.close();
             
            // Closing FileInputStream
            geek.close();
        }
        catch (FileNotFoundException fnfe)
        {
            System.out.println("NO Such File Exists");
        }
        catch (IOException except)
        {
            System.out.println("IOException occurred");
        }
    }
}

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
public String getEncoding()
Parameters : 
Returns :
No value is returned




// Java program illustrating getEncoding() method
 
import java.io.*;
public class NewClass
{
    public static void main(String[] args)
    {
 
        try
        {
            // initializing FileInputStream
            FileInputStream geek = new FileInputStream("ABC.txt");
 
            // Initializing InputStreamReader object
            InputStreamReader in_strm = new InputStreamReader(geek);
 
            // Use of getEncoding() method
            // to get the character encoding present in the stream
            String encoding = in_strm.getEncoding();
 
            System.out.println("Encoding used : "+encoding);
 
            // Closing InputStreamReader
            in_strm.close();
             
            // Closing FileInputStream
            geek.close();
        }
        catch (FileNotFoundException fnfe)
        {
            System.out.println("NO Such File Exists");
        }
        catch (IOException except)
        {
            System.out.println("IOException occurred");
        }
    }
}

Encoding used : UTF8
public int read()
Returns :
Returns single character after reading or -1 if the end of the stream has been reached




// Java program illustrating read() method
 
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws FileNotFoundException, IOException
    {
 
        // initializing FileInputStream
        FileInputStream geek = new FileInputStream("ABC.txt");
 
        // Initializing InputStreamReader object
        InputStreamReader in_strm = new InputStreamReader(geek);
 
        int t;
        String read_reslt="";
 
        // Use of read() method
        while((t = in_strm.read()) != -1)
        {
            read_reslt = read_reslt+(char)t;
        }
     
        // print the result read from the file
        System.out.println(read_reslt);
    }
}

1
Geeks 
2
For 
3
Geeks

Article Tags :