Home »
Java programming language
Java PipedOutputStream connect() Method with Example
PipedOutputStream Class connect() method: Here, we are going to learn about the connect() method of PipedOutputStream Class with its syntax and example.
Submitted by Preeti Jain, on April 17, 2020
PipedOutputStream Class connect() method
- connect() method is available in java.io package.
- connect() method is used to cause this PipedOutputStream to be connected to the given PipedInputStream when this PipedOutputStream is not previously connected to any of the other PipedInputStream.
- connect() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- connect() method may throw an exception at the time of connecting the stream.
IOException: This exception may throw when getting any input/output error or pipe not connected properly, or stream closed.
Syntax:
public void connect(PipedInputStream pis);
Parameter(s):
- PipedInputStream pis – represents the piped input stream to connect to this piped output stream.
Return value:
The return type of the method is void, it returns nothing.
Example:
// Java program to demonstrate the example
// of void connect(PipedInputStream pis) method of
// PipedOutputStream
import java.io.*;
public class ConnectOfPOS {
public static void main(String[] args) throws Exception {
int val = 65;
try {
// Instantiates PipedInputStream and
// PipedOutputStream
PipedInputStream pipe_in = new PipedInputStream();
PipedOutputStream pipe_out = new PipedOutputStream();
// By using connect() method is to
// connect this pipe_out to the given pipe_in
pipe_out.connect(pipe_in);
for (int i = 0; i < 3; ++i) {
// By using write() method is to
// write the val to the stream pipe_out
pipe_out.write(val);
val++;
}
for (int i = 1; i < 4; ++i) {
// By using read() method is to read
// the integer and converted into int
char ch = (char) pipe_in.read();
System.out.println("pipe_in.read(): " + ch);
}
// By using close() method is to close
// the stream
pipe_in.close();
pipe_out.close();
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
Output
pipe_in.read(): A
pipe_in.read(): B
pipe_in.read(): C