java 管道流代码示例
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipedStream {
/**
* @param args
* @throws IOException
*/
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) {
// TODO: handle exception
}
}
}
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) {
// TODO: handle exception
}
}
}
最新文章
- [原]__FILE__宏
- Bootstrap~页面的布局
- 剑指Offer 两个链表的第一个公共结点
- .net环境下ckeditor与ckfinder中文文件链接乱码的问题
- 字节的高低位知识,Ascii,GB2312,UNICODE等编码的关系与来历
- js实现网页图片延时加载的原理和代码 提高网站打开速度
- HTML里面Textarea换行总结
- hdu4006 优先队列
- 纯css3实现tab选项卡
- leetcode[94] Unique Binary Search Trees
- 关于textarea的应用--onchage,onpropertychange,oninput
- qt中线程的使用方法
- 如何使用ILAsm与ILDasm修改.Net exe(dll)文件
- tomcat中的线程问题2
- FastJson:Json树的CRUD操作方法实现
- typeof 与instanceof
- centos7下yum安装mariadb
- windows 下 redis安装
- Matlab警告消息消除
- Spring+Mybatis+Dubbo报错java.lang.reflect.MalformedParameterizedTypeException