Home Java Example Java Io Java IO FilterReader
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java IO FilterReader

Advertisement
In this section we will learn about the FilterReader class in Java.

Java IO FilterReader

In this section we will learn about the FilterReader class in Java.

java.io.FilterReader is an abstract class provides the facility to read filtered character streams. Instance of this class can not be created directly. Subclasses of FilterReader may override methods of this class and may allow extra methods and fields.

Constructor of FilterReader

Constructor Name
Description
FilterReader(Reader in) This constructor creates a new FilterReader.
Syntax : protected FilterReader(Reader in)

Methods in FilterReader

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

    Syntax : public void close() throws IOException
     
  • 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 this stream is supported by mark() method or not.

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

    Syntax : public int read() throws IOException
     
  • read(char[] cbuf, int off, int len) : This method is used to read the specified length of character at the specified position from a specified char array.

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

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

    Syntax
    : public void reset() throws IOException
     
  • skip(long n) : This method is used to skip a specified number of characters.

    Syntax : public long skip(long n) throws IOException

Example

A simple example of FilterReader is being given here. In this example I have created a Java class named JavaFilterReaderExample.java. In this class I have used the FileReader class which provides a file as data source. Further in the core of this class tried to read the characters from the file. To read the the characters used read() method.

Source Code

JavaFilterReaderExample.java

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

class JavaFilterReaderExample extends FilterReader
{
     
   public JavaFilterReaderExample(Reader rdr)
     {
        super(rdr);
     }

   public static void main(String[] args)
    {
      FileReader fr = null; 
            
      try
       {
         fr = new FileReader("file.txt");
         int i;
         while((i= fr.read()) != -1)
            {
               System.out.print((char)i);
            }
       }
      catch(IOException e)
       {
          System.out.println(e);
       }
      finally
       {
            if(fr != null)
              {
                 try
                   {
                      fr.close();
                   }
                  catch(Exception e)
                   {
                       System.out.println(e);
                   }
               }
         }// end finally
      }// end main
  }// end class

Output

When you will execute the above code then the output will be as follows :

Download Source Code

Advertisement

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

Ask Questions?    Discuss: Java IO FilterReader  

Post your Comment


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