Java自学--IO操作(3) 管道流
2013-12-18 10:31
369 查看
import java.io.* ; class Send implements Runnable{ // 线程类 private PipedOutputStream pos = null ; // 管道输出流 public Send(){ this.pos = new PipedOutputStream() ; // 实例化输出流 } public void run(){ String str = "Hello World!!!" ; // 要输出的内容 try{ this.pos.write(str.getBytes()) ; }catch(IOException e){ e.printStackTrace() ; } try{ this.pos.close() ; }catch(IOException e){ e.printStackTrace() ; } } public PipedOutputStream getPos(){ // 得到此线程的管道输出流 return this.pos ; } }; class Receive implements Runnable{ private PipedInputStream pis = null ; // 管道输入流 public Receive(){ this.pis = new PipedInputStream() ; // 实例化输入流 } public void run(){ byte b[] = new byte[1024] ; // 接收内容 int len = 0 ; try{ len = this.pis.read(b) ; // 读取内容 }catch(IOException e){ e.printStackTrace() ; } try{ this.pis.close() ; // 关闭 }catch(IOException e){ e.printStackTrace() ; } System.out.println("接收的内容为:" + new String(b,0,len)) ; } public PipedInputStream getPis(){ return this.pis ; } }; public class PipedDemo{ public static void main(String args[]){ Send s = new Send() ; Receive r = new Receive() ; try{ s.getPos().connect(r.getPis()) ; // 连接管道 }catch(IOException e){ e.printStackTrace() ; } new Thread(s).start() ; // 启动线程 new Thread(r).start() ; // 启动线程 } };
相关文章推荐
- Java自学--IO操作(1)File类
- Java自学之路-Java基础教程-32:Java写文件操作和IO输入输出流
- Java自学--IO操作(4) 打印流
- Java自学--IO操作(2)字节流 字符流
- 黑马程序员——java的IO之流操作的基本规律,Properties类,打印流,序列流
- Java IO操作
- java IO文件操作入门经典例子
- java中IO操作 利用文件流 过滤流对文件进行读写操作
- java-IO操作——使用FileWriter和FileReader来操作文件
- javaIO-字符流-写操作 FileWriter
- java IO操作 主要类继承结构
- java io之管道流
- Java IO操作——专门的数据输入类Scanner(java.util工具包中)的具体使用
- Java中IO操作的小感悟
- Java IO 操作(一)
- JAVA IO操作
- java IO 文件操作---写文件
- IO重定向及管道操作
- Java IO操作——文件操作类File的使用
- Java IO操作——数据操作流DataOutputStream和DataInputStream的使用