31.管道流

管道流

PipedInputStream:字节管道输入流

PipedOutputStream:字节管道输出流

PipedReader:字符管道输入流

PipedWrite:字符管道输出流

示例代码
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

class AThread extends Thread{

    private PipedOutputStream out = null;
    
    public AThread (BThread b) throws IOException{
        out = new PipedOutputStream(b.getIn());
    }

    public void run() {
        try {
            for (int i = 33; i < 65; i++) {
                out.write(i);
            }
            out.close();
        } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
    }
    
}

class BThread extends Thread{

    private PipedInputStream in = new PipedInputStream();
    
    public PipedInputStream getIn(){
        return in;
    }

    public void run() {
        
        int len = -1;
        
        try {
            while((len = in.read()) != -1){
                System.out.println((char)len);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
}

public class PiedDemo {

    public static void main(String[] args) throws Exception {
        
        BThread b = new BThread();
        
        AThread a = new AThread(b);
        
        a.start();
        
        
        b.start();
    }

}

你可能感兴趣的:(31.管道流)