Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Io Java IO Reader

Related Tutorials


 
 

Share on Google+Share on Google+

Java IO Reader

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

Java IO Reader

In this section we will discuss about the Reader class in Java.

Reader class is an abstract class in java.io package provided for reading the character streams. This class is a super class of all the classes which are involved in or to facilitate to read (input) the character streams. Classes descended from this class may overrides the methods of this class but the method read(char[], int, int) and close() must be implemented by the subclasses. This class can not be instantiated directly.

Constructor of Reader

Constructor Name
Description
Reader() This is a default constructor which creates a reader for reading character streams. In such type of character streams reader synchronization of critical sections is depend on the reader itself.
Syntax : protected Reader()
Reader(Object lock) A parameterized constructor which creates a reader for reading character streams. In such type of character streams reader synchronization of critical sections is depend on the given object.
Syntax : protected Reader(Object lock)

Methods of Reader class

Methods that must be implemented by its subclasses are as follows :

  • close() : This method is used to close the stream as well as it is used also for releasing the resources associated with the stream.

    Syntax : public abstract void close() throws IOException
     
  • read(char[] cbuf, int off, int len) : This method is used to read the characters of specified length from an array of characters, started reading from the specified offset 'off'.

    Syntax : public abstract int read(char[] cbuf, int off, int len) throws IOException
     

Commonly used methods of this class are as follows :

  • mark(int readAheadLimit) : This method is used to mark the current position in the stream.

    Syntax : public void mark(int readAheadLimit) throws IOException
     
  • markSupported() : This method returns a boolean value which specifies that whether the mark() operation is supported by this stream or not.

    Syntax : public boolean markSupported()
     
  • read() : This method is used to read the characters (single character).

    Syntax : public int read() throws IOException
     
  • read(char[] cbuf) : This method is used to read characters from an array of characters.

    Syntax : public int read(char[] cbuf) throws IOException
     
  • read(CharBuffer target) : This is used to try to read character from the specified character buffer.

    Syntax : public int read(CharBuffer target) throws IOException
     
  • ready() : This method returns boolean value which specifies whether this stream is ready to be read characters or not.

    Syntax : public boolean ready() throws IOException
     
  • reset() : This method is used to reset the stream.

    Syntax : public void reset() throws IOException
     
  • skip(long n) : This method is used to skip over the characters.

    Syntax : public long skip(long n) throws IOException

Example :

An example of Reader class is given here which demonstrate that how character streams reader can be created and how the characters can be read from the stream. In this example I have created a Java class named JavaReaderExample.java into which created an input stream using Reader and FileReader. To read the contents of file used the read() method of Reader class.

Source Code

JavaReaderExample.java

import java.io.Reader;
import java.io.FileReader;
import java.io.IOException;

public class JavaReaderExample
{
  public static void main(String args[])
   {
      Reader rdr = null;      
      
     try
       {
          rdr = new FileReader("file.txt");
          int c ;
          System.out.println();
          System.out.println("***** OUTPUT *****");
          System.out.println();
          while((c= rdr.read()) != -1)
            {
               System.out.print((char) c);
            }
          System.out.println();
       }
      catch(Exception ex)
       {
          System.out.println(ex);
       }
      finally
       {
          if(rdr != null)
            {
               try
                 {
                    rdr.close();
                 }
               catch(IOException ioe)
                 {
                     System.out.println(ioe);
                 }
            }
       }// end finally
   }// end main
}// end class

Output

When you will execute the above code you will get the output as follows :

Download Source Code

Advertisements

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 14, 2012

Related Tutorials

Discuss: Java IO Reader  

Post your Comment


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