Java - PipedWriter close() method



Description

The Java PipedWriter close() method closes this piped output stream and releases any system resources associated with this stream. This stream may no longer be used for writing characters.

The Java PipedWriter close() method

  • Closes the stream,

  • Releases system resources,

  • Notifies the connected PipedReader that no more characters will be written.

Declaration

Following is the declaration for java.io.PipedWriter.close() method.

public void close()

Parameters

NA

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of PipedWriter close() method

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

         // close the writer
         System.out.println("Closing writer...");
         writer.close();
         System.out.println("Writer closed.");

         // 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 −

Closing writer...
Writer closed.
F
G

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 - Attempting to write after close (shows error)

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("Initial Data");
         writer.close();

         // Attempting to write again after closing the stream
         writer.write("More Data");  // This will throw IOException

      } catch (IOException e) {
         System.out.println("Exception occurred: " + e.getMessage());
      }
   }
}

Output

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

Exception occurred: Pipe closed

Explanation

  • Writing after close() causes an IOException.

  • It demonstrates why close() should only be called when you're truly finished with the stream.

java_io_pipedwriter.htm
Advertisements