Java - PipedReader connect(PipedWriter src) method



Description

The Java PipedReader connect(PipedWriter src) method causes this piped reader to be connected to the piped writer src. If this object is already connected to some other piped writer, an IOException is thrown.

Declaration

Following is the declaration for java.io.PipedReader.connect(PipedWriter src) method.

public void connect(PipedWriter src)

Parameters

src − The piped writer to connect to.

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of PipedReader connect(PipedWriter src) method

The following example shows the usage of PipedReader connect(PipedWriter src) 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 - Connecting PipedReader to PipedWriter and reading text

The following example shows the usage of PipedReader connect(PipedWriter src) 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();

         // Connect the reader to the writer
         reader.connect(writer);

         // Write data through the writer
         writer.write("Hello from PipedWriter!");
         writer.close(); // Close to signal end of data

         // Read and print data from reader
         int data;
         while ((data = reader.read()) != -1) {
            System.out.print((char) data); // Output: Hello from PipedWriter!
         }

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

Output

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

Hello from PipedWriter!

Explanation

  • The connect(PipedWriter src) method connects the PipedReader to the given PipedWriter.

  • Once connected, data written to the PipedWriter can be read from the PipedReader.

  • This is a basic example of one-way character communication between two objects in the same thread.

Example - Inter-thread communication using connect(PipedWriter)

The following example shows the usage of PipedReader connect(PipedWriter src) 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();

      // Connect reader to writer
      reader.connect(writer);

      // Producer thread writes data
      Thread producer = new Thread(() -> {
         try {
            writer.write("Message from Producer Thread");
            writer.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      });

      // Consumer thread reads data
      Thread consumer = new Thread(() -> {
         try {
            int ch;
            while ((ch = reader.read()) != -1) {
               System.out.print((char) ch); // Output: Message from Producer Thread
            }
            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−

Message from Producer Thread

Explanation

  • reader.connect(writer) sets up a pipe between the PipedReader and PipedWriter for communication between threads.

  • The producer thread writes data, and the consumer thread reads it.

  • This is a practical use of connect(PipedWriter) in concurrent programming.

java_io_pipedreader.htm
Advertisements