本帖最后由 love风之吻浩 于 2012-1-9 19:27 编辑
以下是我写管道流的代码:
public static void main(String []args)throws IOException
{
PipedInputStream in=new PipedInputStream();
PipedOutputStream out=new PipedOutputStream();
in.connect(out);
write w=new write(out);
Read r=new Read(in);
new Thread(w).start();
new Thread(r).start();
}
我想不直接通过抛出异常的方式解决异常问题,我尝试着写了以下的代码:
public static void main(String []args)
{
PipedOutputStream out=null;
PipedInputStream in=null;
try{
out=new PipedOutputStream();
in=new PipedInputStream();
in.connect(out);
Xie1 x=new Xie1(out);
Du1 d=new Du1(in);
new Thread(x).start();
new Thread(d).start();
}
catch(IOException e){
throw new RuntimeException("读写失败");
}
finally
{
try{
if(in!=null)
in.close();
}
catch(IOException e){
throw new RuntimeException("关闭失败!");
}
try{
if(out!=null)
out.close();
}
catch(IOException e){
throw new RuntimeException("关闭失败!");
}
}
}
结果运行不了,老是报错,不知是何故,请诸位帮忙解答? |