Java.io - SPLessons

Java.io FilterReader

Chapter 20

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io FilterReader

Java.io FilterReader

shape Introduction

The Java.io FilterReader is the class used to read the characters which are filtered. The Java.io FilterReader is subclass to the reader class and overrides all the methods from the reader class. In filter reader class also have its default methods.

Class Declaration

shape Declaration

Java.io FilterReader class is declared below:

Public abstract class FilterReader extends Reader

shape Fields

Java.io/FilterReader class fields are:

  • Protected Reader in : The field represents the input stream have to read.
  • Protected Object Lock : For synchronize operations this object is used in FilterReader.

Class Constructors

shape Table

Constructor Description
Protected FilterReader (Reader in) The function of this constructor is to create the filer reader instance and used to read from input stream.

Class Methods

shape Table


Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released.
Boolean markSupported () The function of this method is to check the represented mark method is supported by this stream or not.
int read(Char[] c, int off, int n) By using this method it reads the data up to ‘n’ bytes from the character stream into an array of characters.
Long skip (long m) The function of this method is to skip the text or characters in this stream of ‘ m ‘ characters.
int read() The function of this method is to read  only one character.

Inherited Methods

shape Description

From the following classes, methods are inherited to the filter reader class.

  • Java.io.Object
  • Java.io.Reader

shape Examples

Usage of void close() method.

import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
public class FilterReader
{
   public static void main(String[] args) throws Exception 
   {
      
      FilterReader fr = null;
      Reader r = null;
      int i=0;
      char c;
            
      try{
         // creating a new reader
         r = new StringReader("SPLESSONS");
          
         // creating a new filter reader
         fr = new FilterReader(r) {
         };
         
         // To read till to the end of the stream
         while((i=fr.read())!=-1)
         {
            c = (char)i;
            System.out.println(c);
         }
         
      }catch(IOException e)
      {
         
         // if any I/O error occurs
         e.printStackTrace();
      }finally
      {
         
         // releases all the system resources
         if(r!=null)
            r.close();
         if(fr!=null)
         {
            fr.close();
            System.out.print("Stream is closed");
           
         }
      }
   }
}

Output
The result will be as follows.

S
P
L
E
S
S
O
N
S
Stream is closed

Usage of Boolean markSupported () method.

import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;
public class FilterReader
{
   public static void main(String[] args) throws Exception
   {
      
      FilterReader fr = null;
      Reader r = null;      
      boolean bl = false;
      
      try{
         // creating a new reader
         r = new StringReader("SPLESSONS");
          
         // creating a new filter reader
         fr = new FilterReader(r) {
         };
         
         // It will checks the filter reader supports mark() method
         bl=fr.markSupported();
         
         // To print
         System.out.print(" mark() method supported: "+bl);
         
      }catch(Exception e)
      {
         
         // if any I/O error occurs
         e.printStackTrace();
      }finally
      {
         
         // releases all the system resources associated with this stream
         if(r!=null)
            r.close();
         if(fr!=null)
            fr.close();
      }
   }
}

Output
The result will be as follows.

mark() method supported: true

Summary

shape Key Points

  • The Java.io FilterReader is the class used to read the characters which are filtered.
  • The Java.io FilterReader is subclass to the reader class and overrides all the methods from the reader class.