Java PipedWriter Class
The PipedWriter class in Java allows two threads to communicate with each other by passing data through a pipe. This class is useful when we want one part of the program to send data to another part without storing it in memory.
Features of the PipedWriter Class:
- It allows writing data into a pipe.
- It uses a buffer to temporarily store data before sending it to the PipedReader.
- It works with PipedReader to ensure safe data transfer between threads.
- If the pipe breaks, it throws an error.
Declaration of PipedWriter in Java
The Declaration of the PipedWriter class is:
public class PipedWriter extends Writer
All Implemented Interfaces:
- Closeable: This interface is used by classes that handle resources.
- Flushable: This interface is used to flush data to its destination.
- Appendable: This interface is used to append data to an existing stream.
- AutoCloseable: This interface allows automatic closing of resources.
Constructors in PipedWriter Class
This class consists of two constructors, with the help of which we can create objects of this class in different ways. The following are the constructors available in this class:
1. PipedWriter(): This constructor is used to create a PipedWriter, which is not connected to anything yet.
Syntax:
PipedWriter()
Example:
// Demonstrating the working
// of PipedWriter()
import java.io.*;
class Geeks {
public static void main(String[] args) {
// Create PipedWriter and PipedReader
PipedWriter w = new PipedWriter();
PipedReader r = new PipedReader();
try {
// Connect the PipedWriter to the PipedReader
w.connect(r);
// Create a thread to write data into the pipe
Thread writerThread = new Thread(new Runnable() {
public void run() {
try {
w.write("Hello from PipedWriter!");
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
// Create a thread to read data from the pipe
Thread readerThread = new Thread(new Runnable() {
public void run() {
try {
int data;
while ((data = r.read()) != -1) {
System.out.print((char) data);
}
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
// Start both threads
writerThread.start();
readerThread.start();
// Wait for both threads to finish
writerThread.join();
readerThread.join();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
Output
Hello from PipedWriter!
2. PipedWriter(PipedReader inStream): This constructor us used to creates a PipedWriter and connects it to a PipedReader.
Syntax:
PipedWriter(PipedReader snk)
Example:
// Demonstrating the working
// PipedWriter(PipedReader snk)
import java.io.*;
public class Geeks {
public static void main(String[] args) {
try {
// Create a PipedReader and a PipedWriter
PipedReader r = new PipedReader();
PipedWriter w = new PipedWriter(r);
// Create a thread to write data to the PipedWriter
Thread writerThread = new Thread(() -> {
try {
w.write("Hello, PipedWriter");
w.close();
} catch (IOException e) {
e.printStackTrace();
}
});
// Create a thread to read data from the PipedReader
Thread readerThread = new Thread(() -> {
try {
int data;
while ((data = r.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}
});
// Start both threads
writerThread.start();
readerThread.start();
// Wait for both threads to finish
writerThread.join();
readerThread.join();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
Output
Hello, PipedWriter
Java PipedWriter Methods
The image below demonstrates the methods of PipedWriter class.

Now, we are going to discuss about each method one by one in detail:
1. write(int char): This method is used to writes a single character to a pipe.
Syntax:
public void write(int char)
- Parameter: This method take one paramtere, that ischar representing the character to be written.
- Exception: This method return IOException, if there is an issue with the I/O operation.
2. write(char[] carray, int offset, int maxlen): This method is used to write characters from an array to the pipe.
Syntax:
public void write(char[] carray, int offset, int maxlen)
- Parameter: This method includes three parameters which are listed below:
- carray: It is the character array that contains data
- offset: It is the position in the array where to start writing form
- maxlen: It is the maximum number of characters to write.
- Exception: This method return IOException, if there is an issue with the I/O operation.
3. connect(PipedReader destination): This method is used to connects the PipedWriter to a PipedReader.
Syntax:
public void connect(PipedReader destination)
- Parameter: This method takes one parameter destination, it is the PipedReader to which the PipedWriter will connect for data transfer.
- Exception: This method throws IOException if an error occurs while connecting.
4. flush(): This method is used to flush data in the pipe.
Syntax:
public void flush()
- Parameter: This method doesnot take any parameter.
- Exception: This method throws IOException if an error occurs while flushing the data.
5. close(): This method is used to close the PipedWriter.
Synatx:
public void close()
- Parameter: This method doesnot take any parameter.
- Exception: This method throws IOException if there is an issue with closing the writer.
Now, we will discuss how we can use the PipedWriter class to write data and going to read it through a connected PipedReader
Example:
// Demonstrating how to use PipedReader
// and PipedWriter to transferr an array
// of characters between two threads
import java.io.*;
public class Geeks {
public static void main(String[] args) throws IOException {
PipedReader r = new PipedReader();
PipedWriter w = new PipedWriter();
r.connect(w); // Must connect before use
// Writing a char array
char[] c = {'J', 'A', 'V', 'A'};
w.write(c, 0, 4);
// Reading blocks if no data is written yet
System.out.print("Output from the pipe:");
for (int i = 0; i < 4; i++) {
System.out.print(" " + (char) r.read());
}
w.close();
r.close();
}
}
Output
Output from the pipe: J A V A
Java Program Illustrating the Working of PipedWriter Class Methods
Now, we will write some characters, flush the output and close the writer.
Example:
// Java program illustrating the working of PipedWriter
// write(), connect
// close(), flush()
import java.io.*;
public class Geeks {
public static void main(String[] args) throws IOException {
PipedReader r = new PipedReader();
PipedWriter w = new PipedWriter();
try {
// Use of connect(): Connecting the writer to the reader
r.connect(w);
// Use of write(int byte): Writing characters to the pipe
w.write(71);
w.write(69);
w.write(69);
w.write(75);
w.write(83);
// Use of flush() method: Flushing the output to
// make sure all data is written
w.flush();
System.out.println("Output after flush():");
// Reading from the pipe
for (int i = 0; i < 5; i++) {
System.out.print(" " + (char) r.read());
}
// Use of close() method: Closing the writer
System.out.println("\nClosing the Writer stream");
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
Output after flush(): G E E K S Closing the Writer stream