首页 > 代码库 > JavaLearning:JAVA IO Piped流
JavaLearning:JAVA IO Piped流
package org.fun.io; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; class Send implements Runnable{ private PipedOutputStream output=null; public Send(){ this.output=new PipedOutputStream(); } public PipedOutputStream getPipedOutputStream(){ return this.output; } public void run() { String str="hello world!"; try{ this.output.write(str.getBytes()); }catch(IOException e){ e.printStackTrace(); } try{ this.output.close(); }catch(IOException e){ e.printStackTrace(); } } } class Receive implements Runnable{ private PipedInputStream input=null; public Receive(){ this.input=new PipedInputStream(); } public PipedInputStream getPipedInputStream(){ return this.input; } public void run() { byte[] b=new byte[1024]; int len=0; try { len=this.input.read(b); } catch (IOException e) { e.printStackTrace(); } try { input.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println(new String(b,0,len)); } } public class PipedDemo { public static void main(String[] args) throws IOException { Send send=new Send(); Receive rec=new Receive(); send.getPipedOutputStream().connect(rec.getPipedInputStream()); new Thread(send).start(); new Thread(rec).start(); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。