java 管道流
1 package hjw;
2 import java.io.IOException;
3 import java.io.PipedInputStream;
4 import java.io.PipedOutputStream;
5 class Send implements Runnable{
6 private PipedOutputStream pos=null;
7 public Send(){
8 this.pos=new PipedOutputStream();
9 }
10 public void run(){
11 String str="Hello,World!";
12 try{
13 this.pos.write(str.getBytes());
14 } catch (IOException e){
15 e.printStackTrace();
16 }
17 try{
18 this.pos.close();
19 }catch (IOException r){
20 r.printStackTrace();
21 }
22
23 }
24 public PipedOutputStream getPos(){
25 return pos;
26 }
27 }
28 class Receive implements Runnable{
29 private PipedInputStream pis=null;
30 public Receive(){
31 this.pis=new PipedInputStream();
32 }
33 public void run(){
34 byte b[]=new byte[1024];
35 int len=0;
36 try{
37 len=this.pis.read(b);
38 }catch (IOException e){
39 e.printStackTrace();
40 }
41 try{
42 this.pis.close();
43 } catch (IOException e){
44 e.printStackTrace();
45 }
46 System.out.println("接收的内容为:"+new String(b,0,len));
47 }
48 public PipedInputStream getPis(){
49 return pis;
50 }
51 }
52 public class PipedStreamDemo {
53 public static void main(String[] args) {
54 Send s=new Send();
55 Receive r=new Receive();
56 try{
57 s.getPos().connect(r.getPis());
58 }catch (IOException e){
59 e.printStackTrace();
60 }
61 new Thread(s).start();
62 new Thread(r).start();
63 }
64 }