之前在R和shell中用的太多的管道,具体看看jav中的管道是怎么用的,看代码:
package excise;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
class Send implements Runnable {
private PipedInputStream pos = null;
public Send() {
this.pos = new PipedInputStream();
}
public void run() {
String str = "Hello world";
try {
this.pos.write(str.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
try {
this.pos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public PipedInputStream getPos() {
return pos;
}
}
class Receive implements Runnable{
private PipedInputStream pis=null;
public Receive(){
this.pis=new PipedInputStream();
}
public void run(){
byte b[]=new byte[1024];
int len=0;
try{
len=this.pis.read(b);
}catch(Exception e){
e.printStackTrace();
}
try{
this.pis.close();
}catch(Exception e){
e.printStackTrace();
}
}
System.out.println("接受内容为"+new String(b,0,len));
public PipedInputStream getPise(){
return pis;
}
}
public class PipDemo {
public static void main(String args[]){
Send s=new Send();
Receive r=new Receive();
try{
s.getPos().connect(r.getPise());
}catch(Exception e){
e.printStackTrace();
}
new Thread(s).start();
new Thread(r).start();
}
}