IO pipeline flow

 

Package com.yyq;
 Import the java.io. * ;
 / * 
* duct flow 
* a RandomAccessFile 
* a random access file, itself comprising a method of reading and writing 
* by skipBytes (int X) Seek (int X) 
* multithread technology pipe flow 
* properties io + set combination 
* / 
class the Read the implements the Runnable {
 Private the PipedInputStream in; 
the Read (the PipedInputStream in) { 
the this .in = in; 
} 
public  void RUN () {
 the try {
 byte [] buf = new 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;
}
public void run(){
try{
Thread.sleep(6000);
out.write("piped".getBytes());
out.close();
}
catch(Exception e){
throw new RuntimeException("管道写出流失败");
}

}
}
public class PiPedStreamDemo {

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
// 将两个流链接起来 in.connect(out)

in.connect(out);
Read r = new Read(in);
Write w = new Write(out);
new Thread(r).start();
new Thread(w).start();

}

}

 

Guess you like

Origin www.cnblogs.com/daijiabao/p/11226330.html