Java.io.PipedOutputStream.close() Method

Advertisements


Description

The java.io.PipedOutputStream.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 bytes.

Declaration

Following is the declaration for java.io.PipedOutputStream.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

The following example shows the usage of java.io.PipedOutputStream.close() method.

package com.tutorialspoint;

import java.io.*;

public class PipedOutputStreamDemo extends PipedInputStream {

   public static void main(String[] args) {

      // create a new Piped input and Output Stream
      PipedOutputStream out = new PipedOutputStream();
      PipedInputStreamDemo in = new PipedInputStreamDemo();

      try {
         // connect input and output
         out.connect(in);

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

         // close the stream
         System.out.println("Closing Stream...");
         out.close();
         System.out.println("Stream Closed.");

         // print what we wrote
         for (int i = 0; i < 2; i++) {
            System.out.println("" + (char) in.read());
         }


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


   }
}

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

Closing Stream...
Stream Closed.
F
G



Advertisements
Advertisements