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:
Java
// 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();
}
}
}
OutputHello 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:
Java
// 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();
}
}
}
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:
Java
// 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();
}
}
OutputOutput 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
// 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();
}
}
}
OutputOutput after flush():
G E E K S
Closing the Writer stream
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read