package demo.io;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
class 管道流PipedStream {
public static void main(String[] args) {
PipedInputStream input = new PipedInputStream();
PipedOutputStream output = null;
try {
output = new PipedOutputStream(new PipedInputStream());//连接管道流 方法1
// input.connect(output);//连接管道流 方法2
} catch (IOException e) {
e.printStackTrace();
}
new Thread(new Input(input)).start();
new Thread(new Output(output)).start();
}
}
class Input implements Runnable {
private PipedInputStream in;
Input(PipedInputStream in) {
this.in = in;
}
@Override
public void run() {
byte[] buf = new byte[1024];
int len = 0;
try {
in.read(buf, 0, len);
System.out.println(new String(buf, 0, len));
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Output implements Runnable {
private PipedOutputStream out;
Output(PipedOutputStream out) {
this.out = out;
}
@Override
public void run() {
try {
out.write("hei,管道来了".getBytes());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
管道流PipedOutputStream和PipedInputStream
最新推荐文章于 2025-05-14 19:13:02 发布