学习 PipedInputStream PipedOutputStream

本文介绍了Java中的PipedOutputStream和PipedInputStream类,这两种类分别用于管道输出流和管道输入流。文章通过示例代码展示了如何使用这两个类进行数据传输,并解释了它们的主要构造方法和常用方法。

博客地址   http://blog.youkuaiyun.com/ftx2540993425

 

PipedOutputStream类 和PipedInputStream 类为管道输出流 和管道输入流。通常都是以PipedOutputStream作为管道的起始端,通常管道输出流和管道输入流通过connect方法连接起来,实现数据从管道输出流 流入 管道输入流中。管道输出流提供管道输入流的所有字节。

PipedOutputStream类介绍:

构造方法:PipedOutputStream();//创建一个尚未连接到管道输入流的管道输出流。
                  PipedOutputStream(pipedInputStream in);//创建一个连接到该管道输入流的管道输出流。
主要方法。void write(int b);//将指定 byte 写入传送的输出流。
                  void write(byte[] buf);//将该字符数组写入到管道输出流。
                  void close();
                  void connect(PipedInputStream in);使此管道输出流连接到管道输入流 in。
PipedInputStream类介绍:
构造方法:PipedInputStream();//创建一个尚未连接到管道输出流的管道输入流。
                    PipedInputStream(PipedOutputStream in);//创建一个连接到该管道输出入流的管道输入流。
主要方法:int read(byte[] b,int off,int len);//将最多 len 个数据字节从此管道输入流读入 byte 数组
                  void connect(PipedOutputStream out);//使此管道输入流连接到管道输出流 out。
 
简单示例:
 
Test.class
public class Test {  

	/**
	 * @author FTX
	 * @param args
	 * @throws IOException
	 */
    public static void main(String[] args) throws IOException {  
          
        PipedInputStream in = new PipedInputStream();  
        PipedOutputStream out = new PipedOutputStream(in);  
          
        new Thread(new Input(in)).start();  
        new Thread(new Output(out)).start();  
    }  
}  

 

Input.class

class Input implements Runnable{  
      
    private PipedInputStream in;  
  
    public Input(PipedInputStream in) {  
        super();  
        this.in = in;  
    }  
    @Override  
    public void run() {  
        byte []buf = new byte[1024];  
        int len;  
        try {  
            len = in.read(buf);  
            String s = new String(buf,0,len);  
              
            System.out.println("InPut read from Output "+s);  
            in.close();  
        } catch (IOException e) {  
              
            e.printStackTrace();  
        }   
    }  
  
      
}  

Output.class

 

class Output implements Runnable{  
 
private PipedOutputStream out;
 public Output(PipedOutputStream out) { super(); this.out = out; }  
    @Override  
    public void run() {  
        try {  
            out.write("管道流。。。。".getBytes());  
        } catch (IOException e) {  
              
            e.printStackTrace();  
        }     
          
    }  
      
 
}  
 



 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值