Java PipedWriter Class
In this tutorial, we will learn about the PipedWriter
class in Java. Pipes meaning the link between two threads running, and this class is a piped character-output stream. A pipe is said to be broken if a thread that was providing data bytes to the connected piped output stream is no longer alive.
Note: We connect PipedWriter
to a PipedReader
and generally the PipedWriter
and PipedReade
are used by different threads.
Syntax
This is the syntax declaration of PipedWriter
class and we can see it is extending the Writer
class.
public class PipedWriter extends Writer
Constructor
All the variants of PipedWriter
method constructors are given in the table below:
Constructor |
Description |
PipedWriter() |
It creates a piped writer that is not yet connected to a piped reader. |
PipedWriter(PipedReader snk) |
It creates a piped writer connected to the specified piped reader. |
Method
All the methods of PipedWriter
class are given in the table below:
Method |
Description |
void close() |
This method closes this piped output stream and releases any system resources associated with this stream. |
void connect(PipedReader snk) |
This method connects this piped writer to a receiver. |
void flush() |
This method flushes this output stream and forces any buffered output characters to be written out. |
void write(char[] cbuf, int off, int len) |
This method writes len characters from the specified character array starting at offset off to this piped output stream. |
void write(int c) |
This method writes the specified char to the piped output stream. |
Example of Java PipedWriter
Here in this example, we are implementing write()
method of PipedWriter class to write the data. Firstly we created objects of PipedWriter
class and PipedReader
class, then using the connect() method we connected both the methods. Now we are writing the characters to the PipedWriter
and again reading using PipedReader
.
package studytonight;
import java.io.PipedReader;
import java.io.PipedWriter;
public class StudyTonight
{
public static void main(String args[])
{
try
{
PipedReader reader = new PipedReader();
PipedWriter writer = new PipedWriter();
reader.connect(writer);
writer.write(72);
System.out.println((char)reader.read());
writer.write(69);
System.out.println((char)reader.read());
writer.write(76);
System.out.println( (char)reader.read());
writer.write(76);
System.out.println( (char)reader.read());
writer.write(79);
System.out.println( (char)reader.read());
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
H
E
L
L
O
Example of Java PipedWriter
In this example, write() method writes a specified character to the PipedWriter
. Also, we have another overloading method write(char[] arr, int offset, int maxlen)
, In this method, arr is the source array, offset is the index from where it will start writing and maxlen
is the length of the string is to be written.
package studytonight;
import java.io.PipedReader;
import java.io.PipedWriter;
public class StudyTonight
{
public static void main(String args[])
{
try
{
PipedReader reader = new PipedReader();
PipedWriter writer = new PipedWriter();
reader.connect(writer);
char[] arr = {'H', 'E', 'L', 'L', 'O'};
writer.write(arr, 0, 5);
while(true)
{
System.out.print((char) reader.read());
}
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
HELLO
Conclusion:
In this tutorial, we learned about the PipedWriter
class in Java. Pipes meaning the link between two threads running, and this class is a piped character-output stream.