Java FilterWriter
In this tutorial, we will learn about FilterWriter
class in Java. FilterWriter class is an abstract class, it means we cannot instantiate this class. The main purpose of this class is to write filtered character streams. This class provides various methods that pass all the requests to the contained stream. Subclasses of FilterWriter method should override its methods whenever and wherever required to provide more functionality.
Syntax:
The FilterWriter
class is an abstract class and this class extends Writer
class.
public abstract class FilterWriter extends Writer
FilterWriter Constructor
All the constructors of FilterWriter
class is given in the table below.
Constructor |
Description |
protected FilterWriter(Writer out) |
It creates an InputStream class Object. |
FilterWriter Methods
All the methods of FilterWriter class are listed in the table below.
Method |
Description |
void close() |
This method closes the stream, flushing it first. |
void flush() |
This method flushes the stream. |
void write(char[] cbuf, int off, int len) |
This method writes a portion of an array of characters. |
void write(int c) |
This method writes a single character. |
void write(String str, int off, int len) |
This method writes a portion of a string. |
Example of FilterWriter
In this example, we are using the inbuilt write() function of FilterWriter class without adding any extra functionality by overriding it. Firstly we created an object of FileWriter and then we used the FilterWriter to write data in the file, it will write the data inside the file normally since we didn't add any custom methods. We can check the data inside the output.txt file it is the same as we provided.
package studytonight;
import java.io.FileWriter;
import java.io.FilterWriter;
public class StudyTonight
{
public static void main(String args[])
{
try
{
FileWriter fileWriter = new FileWriter("E:\\studytonight\\output.txt");
FilterWriter filterWriter = new FilterWriter(fileWriter) {};
filterWriter.write("Hello Studytonight");
filterWriter.close();
System.out.println("Data is written to the file successfully...");
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
Data is written to the file successfully...
output.txt
Hello Studytonight
In the above program, we implemented the write() method of FilterWriter class as it is, and now it's time to implement it by customizing it.
Example of FilterWriter
In the following example, we are implementing a custom write()
method instead of using the inbuilt method, for that we created a separate class which is extending a FilterWriter
then we override the write method that it will write the text in UPPERCASE without bothering about its actual case. Here we provided the text "Hello Studytonight" but it will write "HELLO STUDYTONIGHT" and we can verify it from the output data in the file.
package studytonight;
import java.io.FileWriter;
import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;
class MyFilter extends FilterWriter
{
MyFilter(Writer out)
{
super(out);
}
public void write(String str) throws IOException
{
super.write(str.toUpperCase());
}
}
public class StudyTonight
{
public static void main(String args[])
{
try
{
FileWriter fileWriter = new FileWriter("E:\\studytonight\\output.txt");
MyFilter customFilterWriter = new MyFilter(fileWriter);
customFilterWriter.write("Hello Studytonight");
customFilterWriter.close();
System.out.println("Data is written to the file successfully...");
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
Data is written to the file successfully...
output.txt
HELLO STUDYTONIGHT
Conclusion
In this tutorial, we learned about FilterWriter
class and its methods. FilterWriter class is an abstract class, which means we cannot instantiate this class. The main purpose of this class is to write filtered character streams.