黑马程序员技术交流社区
标题:
管道流回看
[打印本页]
作者:
奋发吧小白
时间:
2014-10-9 15:02
标题:
管道流回看
package test01;
import java.io.*;
public class Pipe {
public static void main(String[] args) throws Exception
{
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);
ReadPiped r = new ReadPiped(in);
WritePipe w = new WritePipe(out);
new Thread(r).start();
new Thread(w).start();
}
}
class ReadPiped implements Runnable
{
private PipedInputStream in ;
public ReadPiped(PipedInputStream in) {
this.in = in;
}
public void run()
{
try {
byte [] buf = new byte [1024];
int len = in.read(buf);
String s = new String(buf,0,len);
System.out.println(s);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class WritePipe implements Runnable
{
private PipedOutputStream out;
WritePipe(PipedOutputStream out)
{
this.out = out;
}
public void run()
{
try {
out.write("wolaile".getBytes());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2