In this example we will discuss about the FilterWriter in Java.
java.io.FilterWriter is an abstract class allows to write the filtered character streams. Instance of this class can not be created directly. Subclasses of FilterWriter may override methods of this class and may allow extra methods and fields.
Constructor Detail
| FilterWriter(Writer out) | This constructor is used to create a new FilterWriter. Syntax : protected FilterWriter(Writer out) |
Method Detail
Example
Here an example is being given which demonstrates about how to use Java FilterWriter. In this example I have created a Java class into which tried to write the data into the output stream. In this example I have used the various of Java classes of IO package such as BufferedWriter (to write into the stream from buffer), FileWriter( created a file for writing the data) etc.
Source Code
JavaFileWriterExample.java
import java.io.FilterWriter;
import java.io.FileWriter;
import java.io.Writer;
import java.io.BufferedWriter;
import java.io.IOException;
class JavaFilterWriterExample extends FilterWriter
{
public JavaFilterWriterExample(Writer wr)
{
super(wr);
}
public static void main(String[] args)
{
FileWriter fw = null;
BufferedWriter bw = null;
try
{
fw = new FileWriter("file.txt");
bw = new BufferedWriter(fw);
System.out.println();
bw.write("Hello");
bw.newLine();
bw.write("Java IO FilterWriter Example");
System.out.println("Data is written successfully into the file.");
}
catch(IOException e)
{
System.out.println(e);
}
finally
{
if(fw != null)
{
try
{
bw.flush();
bw.close();
fw.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}// end finally
}// end main
}// end class
Output
When you will execute the above example you will get the output as follows :

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Java IO FilterWriter
Post your Comment