Java - OutputStream flush() method



Description

The Java OutputStream flush() method flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

The flush method of OutputStream does nothing.

Declaration

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

public void flush()

Parameters

NA

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of OutputStream flush() method

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

OutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class OutputStreamDemo {
   public static void main(String[] args) {
      try {
         // create a new output stream
         OutputStream os = new FileOutputStream("test.txt");

         // create a new input stream
         InputStream is = new FileInputStream("test.txt");

         // write something
         os.write('A');

         // flush the stream but it does nothing
         os.flush();

         // write something else
         os.write('B');

         // read what we wrote
         System.out.println("" + is.available());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

2

Example - Using flush() with FileOutputStream

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

OutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;

public class OutputStreamDemo {
   public static void main(String[] args) {
      try {
         FileOutputStream fos = new FileOutputStream("example1.txt");

         String data = "Hello, this is a flush example!";
         fos.write(data.getBytes());

         // Flush the output stream to ensure data is written to file
         fos.flush();

         fos.close();
         System.out.println("Data written and flushed to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Data written and flushed to file.

Explanation

  • write() writes the data to an internal buffer.

  • flush() forces any buffered bytes to be written to the file "example1.txt".

  • Without flush(), if the stream wasn't closed properly, some data might remain in the buffer and not reach the file.

Example - Using flush() with BufferedOutputStream

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

OutputStreamDemo.java

package com.tutorialspoint;

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

public class OutputStreamDemo {
   public static void main(String[] args) {
      try {
         FileOutputStream fos = new FileOutputStream("example2.txt");
         BufferedOutputStream bos = new BufferedOutputStream(fos);

         String data = "Buffered stream flush example.";
         bos.write(data.getBytes());

         // Explicitly flush buffer to file
         bos.flush();

         bos.close();
         System.out.println("Buffered data flushed to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Buffered data flushed to file.

Explanation

  • BufferedOutputStream adds buffering to reduce disk I/O.

  • Data written via write() goes into a buffer.

  • flush() ensures all buffered data is pushed to the underlying FileOutputStream.

java_io_outputstream.htm
Advertisements