输入和输出可以直接进行连接,通过结合线程使用(一个线程用于写,另一个用于读,因为管道输入流(读)是要读取管道输出流的数据的,又因为输入流中的read方法是阻塞式的,当两个流在同一个线程中时,输入流的read方法没有数据可以读,就发生阻塞,那么这个线程就挂了)

 import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream; public class Test {
public static void main(String[] args) throws IOException {
PipedInputStream input = new PipedInputStream();
PipedOutputStream output = new PipedOutputStream(); input.connect(output); new Thread(new Input(input)).start();
new Thread(new Output(output)).start();
} }
class Input implements Runnable{ private PipedInputStream in;
Input(PipedInputStream in){
this.in = in;
}
public void run(){ try {
byte[] buf = new byte[1024];
int len = in.read(buf); String s = new String(buf,0,len); System.out.println("s="+s);
in.close();
} catch (Exception e) { } }
} class Output implements Runnable{
private PipedOutputStream out;
Output(PipedOutputStream out){
this.out = out;
}
public void run(){ try {
Thread.sleep(5000);
out.write("hi,管道来了!".getBytes());
} catch (Exception e) { }
}
}

最新文章

  1. ArcSDE10.2.2使用SQL操作ST_Geometry时报ORA-28579
  2. CPU 和内存虚拟化原理 - 每天5分钟玩转 OpenStack(6)
  3. 【小白的CFD之旅】06 流体力学基础
  4. consul 安装
  5. centos 7 相关的一些记录
  6. 高性能网站架构设计之缓存篇(4)- Redis 主从复制
  7. CentOS6.5安装Tab增强版:bash-completion
  8. 扫描函数sweep
  9. cvWaitKey 如果 cvNamedWindow就不会起作用
  10. HTML第二天学习笔记
  11. 转;VC++中Format函数详解
  12. [转]Hulu 2013北京地区校招笔试题
  13. EF+jQueryUI前后端分离设计
  14. 详细分析Java中断机制(转)
  15. Android图片处理神器BitmapFun源码分析
  16. javaSE基础
  17. P2147 [SDOI2008]洞穴勘测(LCT)
  18. Typescript学习总结之泛型
  19. jquery插件-fullpage.js
  20. LightOJ - 1247 Matrix Game (Nim博弈)题解

热门文章

  1. Netty 超时机制及心跳程序实现
  2. Aho-Corasick automaton(AC自动机)解析及其在算法竞赛中的典型应用举例
  3. 数据库MongoDB
  4. java awt学习笔记
  5. .15-浅析webpack源码之WebpackOptionsApply模块-plugin事件流总览
  6. [转]微擎load()文件加载器
  7. UdPloyer交付系统设计思路
  8. 关于vue,webpack 中 “exports is not defined”报错
  9. RSA key format is not supported
  10. Linux 下的 PostgreSQL 数据库+文件通用自动备份脚本