//管道流,多线程和IO相结合
import java.io.*;
class Read implements Runnable
{
private PipedInputStream pis;
Read(PipedInputStream pis)
{
this.pis=pis;
}
public void run()
{
try
{
byte by[]=new byte[1024];
int len=0;
String s=null;
while ((len=pis.read(by))!=-1)
{
s=new String(by,0,len);
}
System.out.println(s);
pis.close();
}
catch (IOException e)
{
throw new RuntimeException("管道读取流失败");
}
}
}
class Write implements Runnable
{
private PipedOutputStream pos;
Write(PipedOutputStream pos)
{
this.pos=pos;
}
public void run()
{
try
{
pos.write("不如不见".getBytes());
pos.close();
}
catch (IOException e)
{
throw new RuntimeException("管道写入流失败");
}
}
}
class PipedInputStreamDemo
{
public static void main(String[] args) throws IOException
{
PipedOutputStream pos=new PipedOutputStream();
PipedInputStream pis=new PipedInputStream(pos);
Read r=new Read(pis);
Write w=new Write(pos);
//pis.connect(pos); 如果构造函数不传入就用此方法。
new Thread(r).start();
new Thread(w).start();
}
}
管道流!
最新推荐文章于 2022-06-16 10:31:27 发布