
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - PipedInputStream close() method
Description
The Java PipedInputStream close() method closes this piped input stream and releases any system resources associated with the stream.
Declaration
Following is the declaration for java.io.PipedInputStream.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 PipedInputStream close() method
The following example shows the usage of PipedInputStream close() method.
PipedInputStreamDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; public class PipedInputStreamDemo { public static void main(String[] args) { // create a new Piped input and Output Stream PipedOutputStream out = new PipedOutputStream(); PipedInputStream in = new PipedInputStream(); try { // connect input and output in.connect(out); // write something out.write(70); out.write(71); // read what we wrote for (int i = 0; i < 2; i++) { System.out.println("" + (char) in.read()); } // close the stream System.out.println("Closing Stream..."); in.close(); System.out.println("Stream Closed."); } catch (IOException ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
F G Closing Stream... Stream Closed.
Example - Closing PipedInputStream after reading data
The following example shows the usage of PipedInputStream close() method.
PipedInputStreamDemo.java
package com.tutorialspoint; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.IOException; public class PipedInputStreamDemo { public static void main(String[] args) { try { // Create and connect piped streams PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pis = new PipedInputStream(pos); // Write some data pos.write("Hello, Pipe!".getBytes()); // Read data from input stream byte[] buffer = new byte[pis.available()]; pis.read(buffer); System.out.println("Read: " + new String(buffer)); // Close the input stream pis.close(); pos.close(); System.out.println("PipedInputStream closed successfully."); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Read: Hello, Pipe! PipedInputStream closed successfully.
Explanation
close() is used to release system resources associated with the input stream.
After reading the data, the input stream is closed to prevent memory leaks or resource exhaustion.
Always closing both ends of a pipe (PipedInputStream and PipedOutputStream) is best practice.
Example - Handling exceptions when writing after PipedInputStream is closed
The following example shows the usage of PipedInputStream close() method.
PipedInputStreamDemo.java
package com.tutorialspoint; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.IOException; public class PipedInputStreamDemo { public static void main(String[] args) { try { PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pis = new PipedInputStream(pos); // Close input stream first pis.close(); System.out.println("PipedInputStream closed."); // Try writing to the pipe after input stream is closed pos.write("Should fail".getBytes()); // This will throw IOException } catch (IOException e) { System.out.println("IOException caught: " + e.getMessage()); } } }
Output
Let us compile and run the above program, this will produce the following result−
PipedInputStream closed. IOException caught: Pipe closed
Explanation
Once PipedInputStream is closed, any attempt to write to the connected PipedOutputStream will throw an IOException.
This helps ensure system integrity and avoids writing to a broken pipe.
Demonstrates why you should carefully coordinate stream closing in multi-threaded or piped stream communication.