
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - PipedOutputStream flush() method
Description
The Java PipedOutputStream flush() method flushes this output stream and forces any buffered output bytes to be written out. This will notify any readers that bytes are waiting in the pipe.
Declaration
Following is the declaration for java.io.PipedOutputStream.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 PipedOutputStream flush() method
The following example shows the usage of PipedOutputStream flush() method.
PipedOutputStreamDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; public class PipedOutputStreamDemo extends PipedInputStream { public static void main(String[] args) { // create a new Piped input and Output Stream PipedOutputStream out = new PipedOutputStream(); PipedOutputStreamDemo in = new PipedOutputStreamDemo(); try { // connect input and output out.connect(in); // write something out.write(70); out.write(71); // flush the stream out.flush(); // print what we wrote for (int i = 0; i < 2; i++) { System.out.println("" + (char) in.read()); } } catch (IOException ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
F G
Example - Using flush() with PipedOutputStream directly
The following example shows the usage of PipedOutputStream flush() method.
PipedOutputStreamDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; public class PipedOutputStreamDemo { public static void main(String[] args) { try { PipedInputStream input = new PipedInputStream(); PipedOutputStream output = new PipedOutputStream(input); // Write data and flush output.write("Hello".getBytes()); output.flush(); // Not strictly necessary, but good form output.close(); int data; while ((data = input.read()) != -1) { System.out.print((char) data); } input.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Hello
Explanation
Here, flush() is called after writing to ensure data is pushed through.
In PipedOutputStream, it's often a no-op but can help signal intent and maintain consistency when used with other OutputStream types.
Example - Using flush() with PrintWriter over PipedOutputStream
The following example shows the usage of PipedOutputStream flush() method.
PipedOutputStreamDemo.java
package com.tutorialspoint; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.PrintWriter; public class PipedOutputStreamDemo { public static void main(String[] args) throws IOException { PipedInputStream input = new PipedInputStream(); PipedOutputStream output = new PipedOutputStream(input); // Wrap PipedOutputStream with PrintWriter PrintWriter writer = new PrintWriter(output); // Consumer thread Thread readerThread = new Thread(() -> { try { BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String line; while ((line = reader.readLine()) != null) { System.out.println("Received: " + line); } reader.close(); } catch (IOException e) { e.printStackTrace(); } }); readerThread.start(); // Write and flush data to the pipe writer.println("Message from producer"); writer.flush(); // Ensures data is immediately available to the reader writer.close(); } }
Output
Let us compile and run the above program, this will produce the following result−
Received: Message from producer
Explanation
This example shows a producer-consumer thread model.
flush() is essential here because PrintWriter buffers output − without flush(), the reader might block waiting for data.
Ensures timely delivery of messages through the pipe.