Java - PipedWriter flush() method



Description

The Java PipedWriter flush() method flushes this output stream and forces any buffered output characters to be written out. This will notify any readers that characters are waiting in the pipe. The flush() method forces any buffered characters to be written to the connected PipedReader. In most simple cases, PipedWriter has minimal buffering, but it's still good practice to use flush() when you want to ensure data is pushed immediately, especially when working with threads.

Declaration

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

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

PipedWriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

public class PipedWriterDemo {
   public static void main(String[] args) {
   
      // create a new Piped writer and reader
      PipedWriter writer = new PipedWriter();
      PipedReader reader = new PipedReader();

      try {
         // connect the reader and the writer
         writer.connect(reader);

         // write something
         writer.write(70);
         writer.write(71);

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

         // print what we wrote
         for (int i = 0; i < 2; i++) {
            System.out.println("" + (char) reader.read());
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

Flushing writer...
Writer flushed.
F
G

Example - Flush ensures immediate data availability (single thread)

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

PipedWriterDemo.java

package com.tutorialspoint;

import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.IOException;

public class PipedWriterDemo {
   public static void main(String[] args) {
      try {
         PipedReader reader = new PipedReader();
         PipedWriter writer = new PipedWriter();
         writer.connect(reader);

         writer.write("Hello");
         writer.flush(); // Forces the characters to the reader

         // Now reader can read immediately
         int ch;
         while ((ch = reader.read()) != -1) {
            System.out.print((char) ch); // Output: Hello
         }

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

Output

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

Hello

Explanation

  • flush() ensures that the "Hello" string is made available to the reader immediately.

  • Without flush() in some buffered writers, the data might be delayed.

Example - Using flush() in a multithreaded producer-consumer setup

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

PipedWriterDemo.java

package com.tutorialspoint;

import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.IOException;

public class PipedWriterDemo {
   public static void main(String[] args) {
      try {
         PipedReader reader = new PipedReader();
         PipedWriter writer = new PipedWriter();
         writer.connect(reader);

         Thread writerThread = new Thread(() -> {
            try {
               writer.write("Thread Message");
               writer.flush(); // Make sure data is sent immediately
               writer.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         });

         Thread readerThread = new Thread(() -> {
            try {
               int ch;
               while ((ch = reader.read()) != -1) {
                  System.out.print((char) ch);
               }
               reader.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         });

         writerThread.start();
         readerThread.start();

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

Output

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

Thread Message

Explanation

  • Writer thread writes and flushes the message.

  • Reader thread immediately receives the flushed message without waiting for the writer to close.

java_io_pipedwriter.htm
Advertisements