package com.tz.util;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import javax.management.RuntimeErrorException;
/**
* 管道流
* @author Administrator
*
*/
public class PipedStreadDemo {
public static void main(String[] args) {
PipedInputStream in=new PipedInputStream();
PipedOutputStream out=new PipedOutputStream();
try {
in.connect(out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Read r=new Read(in);
Write w=new Write(out);
new Thread(r).start();
new Thread(w).start();
}
}
class Read implements Runnable {
private PipedInputStream in;
Read(PipedInputStream in) {
this.in = in;
}
@Override
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);
in.close();
} catch (Exception e) {
throw new RuntimeException("管道失败");
}
}
}
class Write implements Runnable{
private PipedOutputStream out;
Write(PipedOutputStream out){
this.out=out;
}
@Override
public void run() {
try {
out.write("piped lai".getBytes());
out.close();
} catch (Exception e) {
throw new RuntimeException("管道输出失败");
}
}
}