PUBLISHED ON: MARCH 8, 2021
FilterReader read() Method in Java
In this tutorial, we will learn about read()
method of FilterReader class in Java. The task of this method is to read a single character from the given filter reader. This read()
method reads one character at a time from the stream and returns it as an integer value.
Syntax
This is the syntax declaration of the read() method, this method does not receive any parameter but returns data in the integer format ASCII valuers.
public int read()
Example 1: Read Method of FilterReader
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 2: Read Method of FilterReader
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
Conclusion
In this tutorial, we learned about read() method of FilterReader class in Java. The task of this method is to read a single character from the given filter reader. This read()
method reads one character at a time from the stream and returns it as an integer value.