| 发表于:2007-01-31 15:49:44 楼主 |
import java.io.*; public class pipestreamtest { public static void main(string [] args) { try{ thread t1=new sender(); thread t2=new receiver(); pipedoutputstream out=t1.getoutputstream(); pinedinputstream in=t2.getinputstream(); out.connect(in); t1.start(); t2.start(); } catch(exception e) { system.out.println(e.getmessage()); } } } class sender extends thread { private pipedoutputstream out=new pipedoutputstream(); public pipedoutputstream getoutputstream() { return out; } public void run() { string s=new string( "hello,receiver,how are you "); try { out.write(s.getbytes()); out.close(); } catch(exception e) { system.out.println(e.getmessage()); } } } class receiver extends thread { private pipedinputstream in =new pipedinputstream(); public pipedinputstream getinputstream() { return in; } public void run() { string s=null; byte[] buf =new byte[1024]; try { int len=in.read(buf); s=new string (buf,0,len); system.out.println( "the follow message comes from sed=nder:\n "+s); in.close(); } catch(ioexception e) { system.out.println(e.getmessage()); } } } |
|
|
|
|