本帖最后由 来自沙沙的我 于 2014-6-10 20:32 编辑
import java.io.*;
public class IOc_02 {
public static void main(String[] args)throws IOException
{
PipedInputStream aa=new PipedInputStream();
PipedOutputStream bb=new PipedOutputStream();
aa.connect(bb);
Read a=new Read(aa);
Writ b=new Writ(bb);
new Thread(a).start();//进行多线程的运行
new Thread(b).start();
}
}
class Read implements Runnable//创建管道流读取管道类
{
private PipedInputStream aa;
Read(PipedInputStream aa)//引入管道流读取管道
{
this.aa=aa;
}
public void run()//重写run方法,并处理异常
{
try
{
byte[] z=new byte[1024];//创建byte数组存储数据
System.out.println("读取前,管道无闭塞");
int len=aa.read(z);
System.out.println("读取结束");
String s=new String(z,0,len);
System.out.println(s);
aa.close();
}
catch(IOException e)
{
throw new RuntimeException("管道流读取失败");
}
}
}
class Writ implements Runnable//创建读取输出流
{
private PipedOutputStream bb;
Writ(PipedOutputStream bb)
{
this.bb=bb;
}
public void run()
{
try
{
System.out.println("输出前,等待6秒");
Thread.sleep(6000);// 这里系统报错,这么了?!!!!!!!!!!!!!!!!!!!!!!!!!!!
bb.write("zhengzaiduqu".getBytes());//直接输出
bb.close();
}catch(IOException e)
{
throw new RuntimeException("管道流输出失败");
}
}
}
|