Java - Writer flush() method



Description

The Java Writer flush() method flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.

Declaration

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

public abstract void flush()

Parameters

NA

Return Value

This method does not return a value.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of Writer flush() method

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

WriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      String s = "Hello World";

      // create a new writer
      Writer writer = new PrintWriter(System.out);

      try {
         // append a string
         writer.append(s);

         // flush the writer
         writer.flush();

         // append a new string in a new line
         writer.append("\nThis is an example");

         // flush the stream again
         writer.close();

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

Output

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

Hello World
This is an example

Example - Using BufferedWriter with flush()

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

WriterDemo.java

package com.tutorialspoint;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      try {
         Writer writer = new BufferedWriter(new FileWriter("flush1.txt"));
         writer.write("First part of the content.");
         writer.flush();  // Forces the buffer to write to file
         System.out.println("Buffer flushed after first write.");

         writer.write(" Second part added.");
         writer.flush();  // Flush again before closing
         System.out.println("Buffer flushed after second write.");

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

Output

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

Buffer flushed after first write.
Buffer flushed after second write.

Explanation

  • BufferedWriter uses an internal buffer.

  • flush() ensures the content is written immediately to the file, even if the buffer isn't full.

  • Useful when you want to make sure data is written without closing the stream.

Example - Using StringWriter with flush()

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

WriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      try {
         Writer writer = new StringWriter();
         writer.write("Data to memory buffer.");
         writer.flush();  // Flushes buffer (though optional here)
         System.out.println("Contents: " + writer.toString());

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

Output

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

Contents: Data to memory buffer.

Explanation

  • StringWriter writes data to a memory buffer (string-based).

  • Though flush() doesn't have a real effect here, it's still safe and used for consistency when working with Writer subclasses.

  • Always call flush() before close() if you want to ensure all data is written.

java_io_writer.htm
Advertisements