Signup/Sign In

Java FilterReader

In this tutorial, we will learn about FilterReader in Java. The FilterReader class is used to perform a filtering operation on the reader stream. This is a base class when we try to implement our own custom filtering methods for reading. This class overrides all the methods of Reader class. If we extend this class then automatically we will end up extending a Reader class.

Constructor

The constructor of the FilterReader class is given below:

Constructor Description
protected FilterReader(Reader in) It creates a new filtered reader.

Methods

All the methods of the FilterReader class is given below:

Method Description
void close() This method closes the stream and releases any system resources associated with it.
void mark(int readAheadLimit) This method marks the present position in the stream.
boolean markSupported() This method tells whether this stream supports the mark() operation.
boolean ready() This method tells whether this stream is ready to be read.
int read() This method reads a single character.
int read(char[] cbuf, int off, int len) This method reads characters into a portion of an array.
void reset() This method resets the stream.
long skip(long n) This method skips characters.

Example of FilterReader class

In the following example, we are implementing the read() method of FilterReader class to read the data from a file. Here we are using the method as it is without modification so we will get the result the same as it is in the text file. In the next example, we will see how we can customize the read method to read the data in a particular format with our requirements.

package studytonight;
import java.io.FileReader;
import java.io.FilterReader;
import java.io.Reader;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			Reader reader = new FileReader("E:\\studytonight\\output.txt");  
			FilterReader filterReader = new FilterReader(reader) {};  
			int i;  
			while ((i = filterReader.read()) != -1) {  
				System.out.print((char) i);  
			}  
			filterReader.close();  
			reader.close();  
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


Hello Studytonight

output.txt

Hello Studytonight

Example of FilterReader Class

In this example, we will see how we can apply the modification to the methods of FilterReader class by overriding the methods in a class. Here we are going to override the read method, it will return '*' (asterisk) symbol whenever ' ' (space) is occurred. For this firstly we declared the class MyReader and inherited the FilterReader class to it then we override the read method for the logic mentioned above. In the output we can see whenever space has occurred it is returning the asterisk symbol.

package studytonight;
import java.io.FileReader;
import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
class MyReader extends FilterReader
{
	protected MyReader(Reader in) 
	{
		super(in);
	}
	
	public int read() throws IOException
	{  
		int x = super.read();  
		if ((char) x == ' ')  
			return ('*');  
		else  
			return x;  
	}  	
}

public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			Reader reader = new FileReader("E:\\studytonight\\output.txt");  
			MyReader filterReader = new MyReader(reader);  
			int i;  
			while ((i = filterReader.read()) != -1) {  
				System.out.print((char) i);  
			}  
			filterReader.close();  
			reader.close();  
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


Hello*Studytonight

output.txt

Hello Studytonight

Example of FilterReader Class

In this example, we are implementing various methods of FIlterReader class. Here initially we created a reader using StringReader class. Then by using the skip() method of FilterReader class, it will skip reading the first 8 characters and it will point to the 9th character and that's why when we are trying to read then it will start from the 9th character. We can use the ready() method to check whether the reader is ready to read or not. In the program, we are reading eight characters ahead from the 9th position and then after calling the reset() method pointer will come back to the initial position. We can see when we are reading the data after the reset method it is showing from the beginning.

package studytonight;
import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			Reader reader = new StringReader("Welcome to Studytonight"); 
			FilterReader filterReader = new FilterReader(reader){}; 
			char ch[] = new char[28]; 		
			filterReader.skip(8); 
			if(filterReader.ready()) 
			{ 
				filterReader.read(ch); 
				for (int i = 0; i < 8; i++)  
				{ 
					System.out.print(ch[i]); 
				} 
				System.out.print("\n");		

				filterReader.reset(); 
				for (int i = 0; i <7; i++) 
				{ 
					System.out.print((char)filterReader.read()); 
				} 
			} 
			filterReader.close(); 
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


to Study
Welcome

Conclusion

In this tutorial, we learned about the FilterReader method. The FilterReader class is used to perform a filtering operation on the reader stream. This is a base class when we try to implement our own custom filtering methods for reading.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.