Java IO PipedWriter

In this tutorial we will learn about the PipedWriter in Java.

Java IO PipedWriter

In this tutorial we will learn about the PipedWriter in Java.

Java IO PipedWriter

Java IO PipedWriter

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

Constructor
Description
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

  • close() : This method is used to close the stream as well as to release the resources associated with the stream.

    Syntax : public void close() throws IOException
     
  • connect(PipedReader snk) : This method is used to connect the piped writer with the specified piped reader.

    Syntax : public void connect(PipedReader snk) throws IOException
     
  • flush() : This method is used to flush the stream.

    Syntax : public void flush() throws IOException
     
  • write(char[] cbuf, int off, int len) : This method is used to write the specified length of characters started from the specified offset of an array of characters to the stream.

    Syntax : public void write(char[] cbuf, int off, int len)throws IOException
     
  • write(int c) : This method is used to write a character to the stream.

    Syntax : public void write(int c) throws IOException

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 :

Download Source Code