- package test01;
- import java.io.*;
- public class Pipe {
-
- public static void main(String[] args) throws Exception
- {
- PipedInputStream in = new PipedInputStream();
- PipedOutputStream out = new PipedOutputStream();
- in.connect(out);
- ReadPiped r = new ReadPiped(in);
- WritePipe w = new WritePipe(out);
- new Thread(r).start();
- new Thread(w).start();
- }
- }
- class ReadPiped implements Runnable
- {
- private PipedInputStream in ;
- public ReadPiped(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);
- in.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- class WritePipe implements Runnable
- {
- private PipedOutputStream out;
- WritePipe(PipedOutputStream out)
- {
- this.out = out;
- }
- public void run()
- {
-
- try {
- out.write("wolaile".getBytes());
- out.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- }
复制代码
|
|