Java - ObjectStreamWriter flush() method



Description

The Java ObjectStreamWriter flush() method flushes the stream.

Declaration

Following is the declaration for java.io.ObjectStreamWriter.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 ObjectStreamWriter flush() method

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

OutputStreamWriterDemo.java

package com.tutorialspoint;

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

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

         // create a new FileInputStream to read what we write
         FileInputStream in = new FileInputStream("test.txt");

         // write something in the file
         writer.write(70);

         // flush the stream
         System.out.println("Flushing Stream...");
         writer.flush();
         System.out.println("Stream flushed.");

         // read what we write
         System.out.println("" + (char) in.read());

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

Flushing Stream...
Stream flushed.
F

Example - Using flush() to force data write before closing

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

OutputStreamWriterDemo.java

package com.tutorialspoint;

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

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      try {
         FileOutputStream fos = new FileOutputStream("flush_output1.txt");
         OutputStreamWriter writer = new OutputStreamWriter(fos);

         writer.write("Flush ensures data is written immediately.");
         writer.flush();  // Forces characters to be written to file

         // Writer is still open here, more data could be written
         System.out.println("Data flushed to file.");
         writer.close();  // Finally close the stream
      } 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

  • write() writes to an internal buffer.

  • flush() pushes that buffered data to the underlying FileOutputStream immediately, without closing the stream.

  • Useful when you want to make sure data is written while keeping the stream open for more output later.

Example - Flushing data before waiting for user input

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

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.OutputStreamWriter;
import java.io.IOException;
import java.util.Scanner;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      try (
         OutputStreamWriter writer = new OutputStreamWriter(System.out);
         Scanner scanner = new Scanner(System.in)
      ) {
         writer.write("Enter your name: ");
         writer.flush();  // Ensure prompt is shown immediately

         String name = scanner.nextLine();

         writer.write("Hello, " + name + "! Welcome to OutputStreamWriter.\n");
         writer.flush();  // Flush greeting to console

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

Output

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

Enter your name: Anand
Hello, Anand! Welcome to OutputStreamWriter.

Explanation

  • This writes the prompt to System.out, which is a PrintStream wrapped by OutputStreamWriter.

  • Without flush(), the prompt might be delayed or buffered, especially in buffered output streams.

  • flush() ensures that the user sees the prompt right away.

java_io_outputstreamwriter.htm
Advertisements