Java - PipedReader read() method



Description

The Java PipedReader read() method reads the next character of data from this piped stream. If no character is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Declaration

Following is the declaration for java.io.PipedReader.read() method.

public int read()

Parameters

NA

Return Value

This method returns the character read. It returns -1 if there is no more data because the end of the stream has been reached.

Exception

IOException − If an I/O error occurs.

Example - Usage of PipedReader read() method

The following example shows the usage of PipedReader read() method.

PipedReaderDemo.java

package com.tutorialspoint;

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

public class PipedReaderDemo {
   public static void main(String[] args) {
   
      // create a new Piped writer and reader
      PipedWriter writer = new PipedWriter();
      PipedReader reader = new PipedReader();

      try {
         // connect the reader and the writer
         reader.connect(writer);

         // write something
         writer.write(70);
         writer.write(71);

         // read what we wrote
         for (int i = 0; i < 2; i++) {
            System.out.println("" + (char) reader.read());
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

F
G

Example - Reading single characters using read() after connecting to PipedWriter

The following example shows the usage of PipedReader read() method.

PipedReaderDemo.java

package com.tutorialspoint;

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

public class PipedReaderDemo {
   public static void main(String[] args) {
      try {
         PipedReader reader = new PipedReader();
         PipedWriter writer = new PipedWriter(reader); // Connect via constructor

         writer.write("ABC");
         writer.close(); // Finish writing

         int ch;
         while ((ch = reader.read()) != -1) {
            System.out.print((char) ch);  // Output: ABC
         }

         reader.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

ABC

Explanation

  • The read() method reads one character at a time (as an int) and returns -1 when the stream ends.

  • This example demonstrates synchronous reading of characters written to a pipe.

Example - Using read() in a consumer thread (inter-thread communication)

The following example shows the usage of PipedReader read() method.

PipedReaderDemo.java

package com.tutorialspoint;

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

public class PipedReaderDemo {
   public static void main(String[] args) throws IOException {
      PipedReader reader = new PipedReader();
      PipedWriter writer = new PipedWriter();
      reader.connect(writer); // Manual connection

      // Writer thread (producer)
      Thread producer = new Thread(() -> {
         try {
            writer.write("Thread Communication");
            writer.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      });

      // Reader thread (consumer)
      Thread consumer = new Thread(() -> {
         try {
            int ch;
            while ((ch = reader.read()) != -1) {
               System.out.print((char) ch); // Output: Thread Communication
            }
            reader.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      });

      producer.start();
      consumer.start();
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Thread Communication

Explanation

  • read() reads each character sent by the producer thread.

  • This setup simulates a producer-consumer scenario using a pipe.

  • Ensures safe inter-thread communication using character streams.

java_io_pipedreader.htm
Advertisements