day21/PipedStreamDemo.java

来源:互联网 发布:淘宝评价怎么改成匿名 编辑:程序博客网 时间:2024/06/04 18:00
/*集合+IO流  --->Properties类线程+IO流  --->PipedInputStream PipedOutputStream*/import java.io.*;class Read implements Runnable{private PipedInputStream pis;Read(PipedInputStream pis){this.pis = pis;}public void run(){try{byte[] buf = new byte[1024];int len = pis.read(buf);String s = new String(buf,0,len);System.out.println(s);pis.close();}catch (IOException e){throw new RuntimeException("管道读取流失败");}}}class Write implements Runnable{private PipedOutputStream pos;Write(PipedOutputStream pos){this.pos = pos;}public void run(){try{pos.write("piped lai la".getBytes());pos.close();}catch (IOException e){throw new RuntimeException("管道写入流失败");}}}class PipedStreamDemo {public static void main(String[] args) throws IOException{PipedOutputStream pos = new PipedOutputStream();PipedInputStream pis = new PipedInputStream();pis.connect(pos);Write w = new Write(pos);Read r = new Read(pis);new Thread(w).start();new Thread(r).start();}}

0 0
原创粉丝点击