Java IO PipedReader

In this section we will discuss about the PipedReader in Java.

Java IO PipedReader

In this section we will discuss about the PipedReader in Java.

Java IO PipedReader

Java IO PipedReader

In this section we will discuss about the PipedReader in Java.

java.io.PipedReader reads the characters from pipe. java.io.PipedWriter writes the characters to the output stream which is further read by the PipedReader class. So, the input for the PipedReader is a stream which is written by the PipedWriter. To read the characters from the pipe PipedReader is required to connect with the PipedWriter. A connection can be established by the method connection() provided in this class or by the constructor PipedReader(PipedWriter pw) or PipedReader(PipedWriter pw, int pipeSize). Details of constructor and method is being given below.

Constructors of PipedReader

Constructor
Description
PipedReader() This is a default constructor which creates a new PipedReader which is not yet connected.
Syntax : public PipedReader()
PipedReader(int pipeSize) This constructor creates a new PipedReader with the specified pipe size which is not yet connected.
Syntax : public PipedReader(int pipeSize)
PipedReader(PipedWriter src) This constructor creates a new PipedReader which is now connected with the piped writer stream.
Syntax : public PipedReader(PipedWriter src) throws IOException
PipedReader(PipedWriter src, int pipeSize) This constructor creates a new PipedReader with the specified pipe size which is now connected with the piped writer stream.
Syntax : public PipedReader(PipedWriter src, int pipeSize) throws IOException

Methods in PipedReader

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

    Syntax : public void close() throws IOException
     
  • connect(PipedWriter src) : This method is used to connect with the piped writer stream.

    Syntax : public void connect(PipedWriter src) throws IOException
     
  • read() : This method is used to read a character from stream.

    Syntax : public int read() throws IOException
     
  • read(char[] cbuf, int off, int len) : This method is used to read the specified length of characters from the specified characters started at the specified position.

    Syntax : public int read(char[] cbuf, int off, int len) throws IOException
     
  • ready() : This method returns a boolean value which specifies whether this stream is ready to be read or not.

    Syntax : public boolean ready() throws IOException

Example

This is a very simple example which demonstrates about how to use the PipedReader class to read the streams from pipe. As we discussed above to read the stream from pipe the characters should be available in the pipe so, first I have used the PipedWriter class to write the stream into pipe. Then I have tried to read the characters from the piped stream using read() method. This method will read the streams from pipe.

Source Code

PipedReaderExample.java

import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.IOException;

public class PipedReaderExample 
{ 

   public static void main(String[] args)
    {

      PipedWriter pw = null; 
      PipedReader pr = null; 

      try {
              pw = new PipedWriter();
              pr = new PipedReader();
              pr.connect(pw);
              System.out.println();
              // Write by the PipedWriter
              pw.write(82);
              pw.write(79);
	pw.write(83);
	pw.write(69);
	pw.write(73);
	pw.write(78);
	pw.write(68);
	pw.write(73);
	pw.write(65);

              // 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();
                    }
                   catch(Exception e)
                    {
                       System.out.println(e);
                    }
               }
              if(pw != null)
               {
                  try
                    {
                         pw.close();
                    }
                   catch(Exception e)
                    {
                       System.out.println(e);
                    }
               }
          }// close finally
      }// close main
}// close class

Output

When you will execute the above example you will get the output as follows :

Download Source Code