Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - PipedWriter class



Introduction

The Java PipedWriter class represents piped character-output streams.

Class declaration

Following is the declaration for Java.io.PipedWriter class −

public class PipedWriter
   extends Writer

Field

Following are the fields for Java.io.PipedWriter class −

  • protected Object lock − This is the object used to synchronize operations on this stream.

Class constructors

Sr.No. Constructor & Description
1

PipedWriter()

This creates a piped writer that is not yet connected to a piped reader.

2

PipedWriter(PipedReader snk)

This creates a piped writer connected to the specified piped reader.

Class methods

Sr.No. Method & Description
1 void close()

This method closes this piped output stream and releases any system resources associated with this stream.

2 void connect(PipedReader snk)

This method Connects this piped writer to a receiver.

3 void flush()

This method flushes this output stream and forces any buffered output characters to be written out.

4 void write(char[] cbuf, int off, int len)

This method writes len characters from the specified character array starting at offset off to this piped output stream.

5 void write(int c)

This method writes the specified char to the piped output stream.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Writer
  • Java.io.Object

Example - Proper closing after writing

The following example shows the usage of PipedWriter close() 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(reader);

         writer.write("Hello PipedWriter!");
         writer.close();  // Closing after writing

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

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

Output

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

Hello PipedWriter!

Explanation

  • After writing the string, close() is called on the writer.

  • The reader detects the end of the stream (returns -1).

  • This is the standard and safe way to finish a pipe-based communication.

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 - Flush ensures immediate data availability (single thread)

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

         writer.write("Hello");
         writer.flush(); // Forces the characters to the reader

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

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

Output

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

Hello

Explanation

  • flush() ensures that the "Hello" string is made available to the reader immediately.

  • Without flush() in some buffered writers, the data might be delayed.

Advertisements