public class PipedStream {
public static void main(String[] args) throws Exception {
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream();
//使此管道输入流连接到管道输出流
pis.connect(pos);
WritePipe wp = new WritePipe(pos);
ReadPipe rp = new ReadPipe(pis);
new Thread(wp).start();
new Thread(rp).start();
}
}
//写入管道流
class WritePipe implements Runnable
{
private PipedOutputStream pos;
WritePipe(PipedOutputStream pos)
{
this.pos = pos;
}
@Override
public void run() {
try {
pos.write("guan dao liu xie ru".getBytes());
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//从管道流读取
class ReadPipe implements Runnable
{
private PipedInputStream pis;
ReadPipe(PipedInputStream pis)
{
this.pis = pis;
}
@Override
public void run() {
byte[] buf = new byte[1024];
int len = 0;
try {
while((len = pis.read(buf))!=-1)
{
System.out.println(new String(buf,0,len));
}
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
管道流
最新推荐文章于 2013-11-05 14:04:19 发布