In this tutorial we will learn about the PipedWriter in Java.
java.io.PipedWriter writes the characters to the piped output stream. Characters written to the piped output stream can be read by the corresponding piped reader. To read the piped characters PipedReader should be connected with the PipedWriter. PipedWriter(PipedReader pr) and the connect(PipedReader pr) method is used to connect the pipe.
Constructor Detail
| PipedWriter() | This constructor is used to create a new PipedWriter. This piped writer is
not connected to the piped reader yet. Syntax : public PipedWriter() |
| PipedWriter(PipedReader snk) | This constructor is used to create a new PipedWriter which contains the
object of PipedReader. This piped writer is connected to the specified piped
reader now. Syntax : public PipedWriter(PipedReader snk) throws IOException |
Method Detail
Example
Here an example is being given which demonstrate about how to use the PipedWriter. This example explains how to write a specified length of characters started from the specified offset of an array to the piped output stream using PipedWriter write() method.
Source Code
PipedWriterExample.java
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.IOException;
public class PipedWriterExample
{
public static void main(String[] args)
{
char[] ch = {'a','e','i','o','u','r','o','s','e','i','n','d','i','a'};
PipedWriter pw = null;
PipedReader pr = null;
try {
pr = new PipedReader();
pw = new PipedWriter(pr);
System.out.println();
// Write by the PipedWriter
pw.write(ch, 5,9);
// read from the PipedReader
int c;
while((c = pr.read() ) != -1)
{
System.out.print((char) c);
}
}
catch (IOException ioe)
{
System.out.println(ioe);
}
finally
{
if(pr != null)
{
try
{
pr.close();
pw.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}// close finally
}// close main
}// close class
Output
When you will execute this example you will get the output as follows :

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Java IO PipedWriter
Post your Comment