import java.io.*;
class Read implements Runnable{
private PipedInputStream in;
Read(PipedInputStream in){
this.in = in;
}
public void run(){
try{
byte[] buffer = new byte[1024];
int length = in.read(buffer);
String s = new String(buffer,0,length);
System.out.println(s);
in.close();
}catch (IOException){
throw new RuntimeException("管道读取流失败!");
}
}
}
class Write implements Runnable{
private PipedOutputStream out;
Wirte(PipedOutputStream out){
this.out = out;
}
public void run(){
try{
Thread.sleep(3000);
out.write("piped lai le".getBytes());
out.close();
}catch (Exception){
throw new RuntimeException("管道输出流失败!");
}
}
}
class PipedStreamDemo
{
public static void main(String[] args)throws IOException{
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);//两个流接上
Read r = new Read();
Write w = new Write();
new Thread(r).start();
new Thread(w).start();
}
}
IO流--管道流
最新推荐文章于 2024-10-20 15:53:09 发布