Java - PipedWriter connect(PipedReader snk) method



Description

The Java PipedWriter connect(PipedReader snk) method connects this piped writer to a receiver. If this object is already connected to some other piped reader, an IOException is thrown.

This method connects a PipedWriter to a PipedReader, enabling data written by the writer to be read from the reader. This is an alternative to passing the PipedReader to the PipedWriter constructor.

Declaration

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

public void connect(PipedReader snk)

Parameters

snk − The piped reader to connect to.

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of PipedWriter connect(PipedReader snk) method

The following example shows the usage of PipedWriter connect(PipedReader snk) method.

PipedWriterDemo.java

package com.tutorialspoint;

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

public class PipedWriterDemo {
   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
         writer.connect(reader);

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

         // print 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 - Connect using connect() method (sequential communication)

The following example shows the usage of PipedWriter connect(PipedReader snk) method.

PipedWriterDemo.java

package com.tutorialspoint;

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

public class PipedWriterDemo {
   public static void main(String[] args) {
      try {
         PipedReader reader = new PipedReader();
         PipedWriter writer = new PipedWriter();

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

         writer.write("Hello, world!");
         writer.close(); // Important: signals end of data

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

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

Output

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

Hello, world!

Explanation

  • The writer is connected to the reader using connect().

  • Characters written to the writer can be read from the reader.

  • close() marks the end of stream so read() exits correctly.

Example - Connect using connect() in a multithreaded setup

The following example shows the usage of PipedWriter connect(PipedReader snk) method.

PipedWriterDemo.java

package com.tutorialspoint;

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

public class PipedWriterDemo {
   public static void main(String[] args) {
      try {
         PipedReader reader = new PipedReader();
         PipedWriter writer = new PipedWriter();
         writer.connect(reader); // Connect them

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

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

         writerThread.start();
         readerThread.start();

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

Output

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

Message from writer thread.

Explanation

  • The connect() method is used before starting the threads.

  • The writer sends data in one thread, and the reader receives it in another.

  • This is a classic use case of PipedReader/PipedWriter for thread communication.

java_io_pipedwriter.htm
Advertisements