A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

今天看到毕老师第21的视频,模仿视频写了一个使用管道流的程序

但是抛出了两个异常,自己解决了一个,该有一个我找不到解决的办法,大神帮我看看怎么解决。。

3Q
  1. package day21;

  2. import java.io.IOException;
  3. import java.io.PipedInputStream;
  4. import java.io.PipedOutputStream;

  5. class ReaderPip implements Runnable {
  6.         private PipedInputStream pis;

  7.         ReaderPip(PipedInputStream pis) {
  8.                 this.pis = pis;
  9.         }

  10.         public void run() {
  11.                 try {
  12.                         byte[] bt = new byte[1024];
  13.                         int len = 0;
  14.                         while ((len = pis.read(bt)) != -1) {
  15.                                 String str = new String(bt, 0, len);
  16.                                 System.out.println(str);
  17.                         }
  18.                 } catch (IOException e) {
  19.                         e.printStackTrace();
  20.                         // throw new RuntimeException("reader fail");
  21.                 } finally {
  22.                         try {
  23.                                 pis.close();

  24.                         } catch (IOException e) {
  25.                                 e.printStackTrace();
  26.                         }
  27.                 }

  28.         }
  29. }

  30. class WritePip implements Runnable {
  31.         private PipedOutputStream pos;

  32.         WritePip(PipedOutputStream pos) {
  33.                 this.pos = pos;
  34.         }

  35.         public void run() {
  36.                 try {
  37.                         pos.write("嘿咻嘿咻。。。".getBytes());
  38.                        
  39.                 } catch (IOException e) {
  40.                         e.printStackTrace();
  41.                 } finally {
  42.                         try {
  43.                                 pos.close();
  44.                         } catch (IOException e) {
  45.                                 e.printStackTrace();
  46.                         }
  47.                 }
  48.         }
  49. }

  50. public class PipedStreamDemo {
  51.         public static void main(String args[]) {
  52.                 PipedInputStream pis = new PipedInputStream();
  53.                 PipedOutputStream pos = new PipedOutputStream();
  54.                 try {
  55.                         pis.connect(pos);
  56.                         new Thread(new ReaderPip(pis)).start();
  57.                         new Thread(new WritePip(pos)).start();

  58.                 } catch (IOException e) {
  59.                         e.printStackTrace();
  60.                 } finally {
  61.                         try {
  62.                                 pis.close();
  63.                         } catch (IOException e) {
  64.                                 e.printStackTrace();
  65.                         } finally {
  66.                                 try {
  67.                                         pos.close();
  68.                                 } catch (IOException e) {
  69.                                         e.printStackTrace();
  70.                                 }
  71.                         }
  72.                 }
  73.         }
  74. }
复制代码
异常提醒如下:
java.io.IOException: Pipe closed
        at java.io.PipedInputStream.checkStateForReceive(PipedInputStream.java:244)
        at java.io.PipedInputStream.receive(PipedInputStream.java:210)
        at java.io.PipedOutputStream.write(PipedOutputStream.java:132)
        at java.io.OutputStream.write(OutputStream.java:58)
        at day21.WritePip.run(PipedStreamDemo.java:46)
        at java.lang.Thread.run(Thread.java:619)

点评

FFF
虽然你的"嘿咻嘿咻"很邪恶,但是还是有活动的额外技术分+1  发表于 2013-11-21 18:25

评分

参与人数 1技术分 +2 收起 理由
FFF + 2 活动额外技术分+1

查看全部评分

7 个回复

倒序浏览
更改后的代码如下:
class ReaderPip implements Runnable {
        private PipedInputStream pis;

        ReaderPip(PipedInputStream pis) {
                this.pis = pis;
        }

        public void run() {
                try {
                        byte[] bt = new byte[1024];
                        int len = 0;
                        while ((len = pis.read(bt)) != -1) {
                                String str = new String(bt, 0, len);
                                System.out.println(str);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                        // throw new RuntimeException("reader fail");
                } finally {
                        try {
                                pis.close();

                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }

        }
}

class WritePip implements Runnable {
        private PipedOutputStream pos;

        WritePip(PipedOutputStream pos) {
                this.pos = pos;
        }

        public void run() {
                try {
                        pos.write("嘿咻嘿咻。。。".getBytes());
                        
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        try {
                                pos.close();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
        }
}

public class PipedStreamDemo {
        public static void main(String args[]) {
                PipedInputStream pis = new PipedInputStream();
                PipedOutputStream pos = new PipedOutputStream();      
                        try {
                                                        pis.connect(pos);
                                                } catch (IOException e) {
                                                        // TODO Auto-generated catch block
                                                        e.printStackTrace();
                                                }
                        new Thread(new ReaderPip(pis)).start();
                        new Thread(new WritePip(pos)).start();

        }
}
原因是因为你前面已经把流关闭了,后面又把流关闭了一次

点评

FFF
下次记得代码要写代码框里~~~不然不给你技术分了!  发表于 2013-11-21 18:27
FFF
骚年,告诉我~你真的不是被"嘿咻嘿咻"吸引过来的~~~  发表于 2013-11-21 18:27

评分

参与人数 1技术分 +2 收起 理由
FFF + 2 活动额外技术分+1

查看全部评分

回复 使用道具 举报
终结者 发表于 2013-11-21 14:06
更改后的代码如下:
class ReaderPip implements Runnable {
        private PipedInputStream pis;

这个真没注意到

谢了 哥们,,
回复 使用道具 举报
FFF 金牌黑马 2013-11-21 18:26:18
板凳
如果问题已经解决,请及时修改主题为“提问结束”。
修改主题的方法链接
http://bbs.itheima.com/thread-89313-1-1.html
如果没有解决,可能你的问题问得不够清楚。可以重新发问的哦~
回复 使用道具 举报
FFF 发表于 2013-11-21 18:26
如果问题已经解决,请及时修改主题为“提问结束”。
修改主题的方法链接
http://bbs.itheima.com/thread-89 ...

3F哥  3Q for zhe JiShuFen
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马