Java - ObjectOutputStream flush() method



Description

The Java ObjectOutputStream flush() method flushes the stream. This will write any buffered output bytes and flush through to the underlying stream. The flush() method forces any buffered output bytes to be written out to the destination (like a file or socket). It ensures that all the data written so far is actually pushed out of the stream buffer.

Declaration

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

public void flush()

Parameters

NA

Return Value

This method does not return a value.

Exception

IOException − If an I/O error has occurred.

Example - Usage of ObjectOutputStream flush() method

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

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      int i = 319874;
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeInt(i);
         oout.writeInt(1653984);

         // flush the stream
         oout.flush();

         // close the stream
         oout.close();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print an int
         System.out.println("" + ois.readInt());

         // read and print an int
         System.out.println("" + ois.readInt());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

319874
1653984

Example - Using flush() to ensure data is immediately written to file

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

ObjectOutputStreamDemo.java

package com.tutorialspoint;

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

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      String filename = "flush_example1.ser";

      try (FileOutputStream fos = new FileOutputStream(filename);
         ObjectOutputStream oos = new ObjectOutputStream(fos)) {

         oos.writeObject("Hello from flush()");
         oos.flush(); // Make sure data is pushed to the file immediately

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

Output

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

Data flushed to file.

Explanation

  • We write a String to the file.

  • flush() ensures it's physically written before the stream is closed.

  • This is especially useful if you're writing data in chunks, or want to ensure data is written before a crash or delay.

Example - Using flush() before keeping the stream open (e.g., over a network/socket)

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

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      try (PipedOutputStream pos = new PipedOutputStream();
         PipedInputStream pis = new PipedInputStream(pos);
         ObjectOutputStream oos = new ObjectOutputStream(pos);
         ObjectInputStream ois = new ObjectInputStream(pis)) {

         oos.writeObject("Message 1");
         oos.flush(); // Send immediately — no buffering delay
         System.out.println("Message 1 sent.");

         String received = (String) ois.readObject();
         System.out.println("Message received: " + received);

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

Output

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

Message 1 sent.
Message received: Message 1

Explanation

  • This simulates sending data through a pipe (like a socket).

  • flush() is critical here to avoid buffering delays — otherwise, the receiver might block waiting for data.

  • This is how flush() is typically used in networked applications.

java_io_objectoutputstream.htm
Advertisements