Java - FilterWriter flush() method



Description

The Java FilterWriter flush() method forces any buffered characters to be written immediately to the underlying stream or file. It ensures that data is not left in the buffer before closing or reading. Unlike close(), flush() keeps the stream open for further writing.

Declaration

Following is the declaration for java.io.FilterWriter.flush() method −

public void flush()

Parameters

NA

Return Value

The method does not return any value.

Exception

IOException − If an I/O error occurs.

Example - Usage of FilterWriter flush() method

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

FilterWriterDemo.java

package com.tutorialspoint;

import java.io.FilterWriter;
import java.io.StringWriter;
import java.io.Writer;

public class FilterWriterDemo {
   public static void main(String[] args) throws Exception {
      FilterWriter fw = null;
      Writer w = null;
      String s = null;
      
      try {
         // create new reader
         w = new StringWriter(6);
          
         // filter writer
         fw = new FilterWriter(w) {
         };
         
         // write to filter writer
         fw.write(65);

         // flushes the content
         fw.flush();
         System.out.println("flush() invoked");
         System.out.println("flushes content to the writer");

         // get the string
         s = w.toString();
         
         // print
         System.out.print("String: "+s);
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(w!=null)
            w.close();
         if(fw!=null)
            fw.close();
      }
   }
}

Output

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

flush() invoked
flushes content to the writer
String: A

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 - Using flush() in PrintWriter to Ensure Immediate Writing

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

FilterWriterDemo.java

package com.tutorialspoint;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class FilterWriterDemo {
   public static void main(String[] args) {
      try (PrintWriter fw = new PrintWriter(new FileWriter("data.txt"))) {
         fw.write("Java FilterWriter Example");

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

         fw.write("\nMore data after flushing...");

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

Output

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

Data flushed successfully.

Explanation

  • Uses PrintWriter, which extends FilterWriter.

  • Writes "Java FilterWriter Example" to "data.txt", but data may still be buffered.

  • Calls flush() to ensure immediate writing before continuing.

  • Writes additional text after flushing to demonstrate that the stream is still open.

java_io_filterwriter.htm
Advertisements