Java - FilterOutputStream flush() method



Description

The Java FilterOutputStream flush() method forces any buffered output bytes to be written immediately to the underlying output stream. This ensures that no data is left in the buffer before closing the stream. Prevents data loss in case of abrupt termination.

Declaration

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

public void flush()

Parameters

NA

Return Value

This method does not return any value.

Exception

  • IOException − If any I/O error occurs.

Example - Usage of FilterOutputStream flush() method

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

FilterOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FilterOutputStreamDemo {
   public static void main(String[] args) throws Exception {
      OutputStream os = null; 
      FilterOutputStream fos = null;
      FileInputStream fis = null;
      int i = 0;
      char c;
      
      try {
         // create output streams
         os = new FileOutputStream("test.txt");
         fos = new FilterOutputStream(os);

         // writes byte to the output stream
         fos.write(65);
                  
         // forces byte contents to written out to the stream
         fos.flush();
         
         // create output streams
         fis = new FileInputStream("test.txt");
         
         // read byte
         i=fis.read();
         
         // convert integer to characters
         c = (char)i;
         
         // prints
         System.out.print("Character read: "+ c);
         
      } catch(IOException e) {
         // if any I/O error occurs
         System.out.print("Close() is invoked prior to write()");
      } finally {
         // releases any system resources associated with the stream
         if(os!=null)
            os.close();
         if(fos!=null)
            fos.close();
      }
   }
}

Output(assuming test.txt contains ABCDEF)

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

Character read: A

Example - Using flush() with BufferedOutputStream

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

FilterOutputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;

public class FilterOutputStreamDemo {
   public static void main(String[] args) {
      try (FilterOutputStream fos = new BufferedOutputStream(new FileOutputStream("output.txt"))) {
         String data = "Hello, FilterOutputStream!";
         fos.write(data.getBytes()); // Write data to buffer

         fos.flush(); // Ensure data is written to the file immediately
         System.out.println("Data flushed successfully.");
      } 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 BufferedOutputStream, a subclass of FilterOutputStream.

  • Writes string data to "output.txt", but data is buffered.

  • Calls flush() to immediately write buffered data to the file.

Example - Using flush() in DataOutputStream to Ensure Data Integrity

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

FilterOutputStreamDemo.java

package com.tutorialspoint;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;

public class FilterOutputStreamDemo {
   public static void main(String[] args) {
      try (FilterOutputStream fos = new DataOutputStream(new FileOutputStream("data.bin"))) {
         fos.write(65); // Writes ASCII 'A'
         fos.write(66); // Writes ASCII 'B'
         fos.write(67); // Writes ASCII 'C'

         fos.flush(); // Forces the data to be written
         System.out.println("Binary data flushed successfully.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Binary data flushed successfully.

Explanation

  • Uses DataOutputStream, which extends FilterOutputStream.

  • Writes binary data (65 → 'A', 66 → 'B', 67 → 'C') to "data.bin", but data may be buffered.

  • Calls flush() to immediately write data before closing the stream.

java_io_filteroutputstream.htm
Advertisements