黑马程序员技术交流社区
标题:
管道流的连接的小问题
[打印本页]
作者:
王浩龙
时间:
2014-3-18 11:45
标题:
管道流的连接的小问题
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
class Read implements Runnable{
private PipedInputStream in;
Read(PipedInputStream in){
this.in = in;
}
@Override
public void run() {
byte[] b = new byte[1024];
int len = 0;
try {
System.out.println("xiuxiyixia 6miao");
Thread.sleep(6000);
len = in.read(b);
System.out.println(new String(b, 0, len));
} catch (Exception e) {
throw new RuntimeException("管道流读取失败");
}
finally{
if(in!=null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class Write implements Runnable{
private PipedOutputStream out;
Write(PipedOutputStream out){
this.out = out;
}
@Override
public void run() {
try {
out.write("asdasdasfwq".getBytes());;
} catch (IOException e) {
throw new RuntimeException("管道流写入取失败");
}
finally{
if(out!=null)
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class PipedStreamTest {
public static void main(String[] args) throws IOException {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);//这里的我们可以用out去连接in吗?
Read r = new Read(in);
Write w = new Write(out);
new Thread(r).start();
new Thread(w).start();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2