学习 PipedInputStream PipedOutputStream 改进版
2015-12-09 10:44
651 查看
package com.ftx; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; public class Pipe { /** * @author FTX * @param args * @throws IOException */ public static void main(String[] args) throws IOException { PipedInputStream in = new PipedInputStream(); PipedOutputStream out = new PipedOutputStream(in); new Thread(new Input(in)).start(); new Thread(new Output(out)).start(); try { new Thread(new Output(out)).sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class Input implements Runnable { private PipedInputStream in; public Input(PipedInputStream in) { super(); this.in = in; } @Override public void run() { byte[] buf = new byte[1024]; int len; String s = null; for (int i = 0; i < 5; i++) { try { len = in.read(buf); if (len != -1) { s = new String(buf, 0, len); } System.out.println("Input read " + s); } catch (IOException e) { e.printStackTrace(); } } try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class Output implements Runnable { @Override public void run() { for (int i = 0; i < 5; i++) { try { out.write((i + "管道流").getBytes()); out.flush(); } catch (IOException e) { e.printStackTrace(); } } try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private PipedOutputStream out; public Output(PipedOutputStream out) { super(); this.out = out; } }
改进版,可以多个输入,读出。
博客地址 http://blog.csdn.net/ftx2540993425
相关文章推荐
- php的密码加密password_hash
- PHP 常用header头部定义汇总
- PHP无限极分类之数组
- 提高php性能细节
- spark MapOutputTrackerMaster
- ContentProvider往通讯录添加联系人和获取联系人
- php与二维码
- PHPStorm 10 激活
- php set_time_limit
- Magento Zend framework通过smtp发送邮件
- YII framewoke
- thinkphp3.2.3
- Web攻防系列教程之 PHP + Mysql注入攻防实战(转载)
- ContentProvider的案例
- php配置php-fpm启动参数及配置详解
- 在php中调用接口以及编写接口
- 学习 PipedInputStream PipedOutputStream
- ThinkPHP 模板中IF的使用
- php中heredoc的使用方法
- 关于PHP动态转静态减轻压力的方法