java 管道流代码示例
2013-11-21 14:33
288 查看
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
}
}
}
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
}
}
}
相关文章推荐
- 【转】一个非常标准的Java连接Oracle数据库的示例代码
- Java 批量文件压缩导出并下载到本地示例代码
- java 输入和输出 代码示例
- 关于Java中多线程死锁的代码示例
- java中操作xml几种方式的比较和代码示例(DOM,SAX,JDOM,DOM4J)
- MemCached的安装和JAVA客户端连接Memcached示例代码
- java线程间通信的通俗解释及代码示例
- Java:集合框架详解(ArrayList)和代码示例
- Java NIO 非阻塞式(non-block) 代码示例
- 更新整理本人所属博客文章的示例代码和工具组件(Java 和 C++)
- Java一些特性的示例代码
- Java加密解密和数字签名完整代码示例
- Java中普通代码块,构造代码块,静态代码块区别及代码示例
- Java中对List集合内的元素进行顺序、倒序、随机排序的示例代码
- spring框架整合使用定时任务框架java quartz的示例代码配置
- 在java中使用dom4j解析xml(示例代码)
- java读取csv文件内容示例代码
- Java中普通代码块,构造代码块,静态代码块区别及代码示例
- java简单实现八叉树图像处理代码示例
- Java用文件流下载网络文件示例代码