Java - FilterWriter Class



Introduction

The Java FilterWriter class is for writing filtered character streams. Following are the important points about FilterWriter −

  • The class itself provides default methods that pass all requests to the contained stream.

  • The Subclasses of FilterWriter should override some of these methods and may also provide additional methods and fields.

Class declaration

Following is the declaration for Java.io.FilterWriter class −

public abstract class FilterWriter
   extends Writer

Field

Following are the fields for Java.io.FilterWriter class −

  • protected Writer in − This is the character-output stream.

  • protected Object lock − This is the object used to synchronize operations on this stream.

Class constructors

Sr.No. Constructor & Description
1

protected FilterWriter(Writer in)

This creates a new filtered writer.

Class methods

Sr.No. Method & Description
1 void close()

This method flushes the stream.

2 void flush()

This method flushes the stream.

3 void write(char[] cbuf, int off, int len)

This method writes a portion of an array of characters.

4 void write(int c)

This method writes a single character.

5 void write(String str, int off, int len)

This method writes a portion of a string.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Writer
  • Java.io.Object

Example - Using close() with BufferedWriter (Automatic Closure)

The following example shows the usage of Java FilterWriter close() method.

FilterWriterDemo.java

package com.tutorialspoint;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FilterWriterDemo {
   public static void main(String[] args) {
      try (BufferedWriter fw = new BufferedWriter(new FileWriter("output.txt"))) {
         fw.write("Hello, FilterWriter!");
         System.out.println("Data written successfully.");
         // No need to manually close, try-with-resources handles it
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Data written successfully.

Explanation

  • Uses BufferedWriter, which is a subclass of FilterWriter.

  • Writes "Hello, FilterWriter!" to "output.txt".

  • Uses try-with-resources, which automatically calls close() at the end.

Example - Using flush() with BufferedWriter

The following example shows the usage of Java FilterWriter flush() method.

FilterWriterDemo.java

package com.tutorialspoint;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FilterWriterDemo {
   public static void main(String[] args) {
      try (BufferedWriter fw = new BufferedWriter(new FileWriter("output.txt"))) {
         fw.write("Hello, FilterWriter!");

         fw.flush(); // Ensures data is written to the file immediately
         System.out.println("Data flushed successfully.");

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Hello")

Let us compile and run the above program, this will produce the following result−

Data flushed successfully.

Explanation

  • Uses BufferedWriter, a subclass of FilterWriter.

  • Writes "Hello, FilterWriter!" to "output.txt", but data may still be buffered.

  • Calls flush() to ensure immediate writing to the file.

Example - Writing Individual Characters Using BufferedWriter

The following example shows the usage of Java FilterWriter write(int c) method.

FilterWriterDemo.java

package com.tutorialspoint;

import java.io.FileWriter;
import java.io.FilterWriter;
import java.io.IOException;

public class FilterWriterDemo {
   public static void main(String[] args) {
      try (FilterWriter fw = new FilterWriter(new FileWriter("output.txt")){}) {
         fw.write(72); // ASCII for 'H'
         fw.write(101); // ASCII for 'e'
         fw.write(108); // ASCII for 'l'
         fw.write(108); // ASCII for 'l'
         fw.write(111); // ASCII for 'o'

         System.out.println("Characters written successfully to output.txt.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Characters written successfully to output.txt.

Explanation

  • Uses BufferedWriter, a subclass of FilterWriter.

  • Writes characters one by one using their ASCII values

    • 72 → 'H'

    • 101 → 'e'

    • 108 → 'l'

    • 108 → 'l'

    • 111 → 'o'

  • The file stores these characters as text.

Advertisements