Home Java Example Java Io Java IO InputStreamReader

Related Tutorials


 
 

Share on Google+Share on Google+

Java IO InputStreamReader

Advertisement
In this section we will discuss about the InputStreamReader in Java.

Java IO InputStreamReader

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

Constructor
Description
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

  • close() : This method is used to close the stream as well to release the system resources associated with the stream.

    Syntax : public void close() throws IOException
     
  • getEncoding() : This method is used to get the character encoding which is used by the stream.

    Syntax : public String getEncoding()
     
  • read() : This method is used to read characters from the stream.

    Syntax : public int read() throws IOException
     
  • read(char[] cbuf, int offset, int length) : This method is used to read the specified length of characters from the specified char array buffer started at the specified position.

    Syntax : public int read(char[] cbuf, int offset, int length) throws IOException
     
  • ready() : This method returns a boolean value which specifies whether this stream is ready to be read or not.

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 :

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 17, 2012

Related Tutorials

Discuss: Java IO InputStreamReader  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com