p { margin-bottom: 0.21cm; }a:link { color: rgb(0, 0, 255); }
PipedInputStream 类 和 PipedOutputStream 类用于在程序中创建管道通信
使用管道流内可以实现各个模块之内的松耦合通信 即 管道用来把一个程序、线程和代码块的输出连接到另一个程序、线程和代码块的输入 管道流仅仅在内存中使用
使用的使用两个类必须用connect 方法 进行连接 可以PipedInputStream 连接PipedOutputStream 也可以PipedOutputStream 连接PipedInputStream
connect ( PipedOutputStream src)
使此传送输入流连接到传送输出流 src 。
管道流必须在发送对象和接收对象之前进行连接
管道流使用实例
public class MyPipedStreamTest {
public PipedOutputStream pipedOutputStream = new PipedOutputStream();
public PipedInputStream pipedInputStream = new PipedInputStream();
public static void main(String[] args) throws Exception {
MyPipedStreamTest piped = new MyPipedStreamTest();
piped. pipedInputStream .connect(piped. pipedOutputStream );
piped. pipedOutputStream .write( " 发送给输出管道的内容 " .getBytes());
// 输入管道连接输出管道
// 读出接受到的数据
byte [] buf = new byte [1024];
int len = piped. pipedInputStream .read(buf);
System. out .println( " 读到的数据有 " + new String(buf,0,len));
}
}