首页 > 其他 > 详细

IO//管道流

时间:2019-11-07 16:00:09      阅读:68      评论:0      收藏:0      [点我收藏+]

管道流 : PipedInputStream   、 PipedOutputStream

import java.io.*;
import java.util.*;
public class Practice_2 {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub 
        
        PipedInputStream in = new PipedInputStream();
        PipedOutputStream out = new PipedOutputStream();
        in.connect(out);
        
        Read r = new Read(in);
        Write w = new Write(out);
        
        new Thread(r).start();
        new Thread(w).start();
        
    }
    
}
class Read implements Runnable
{
    private PipedInputStream in;
    Read(PipedInputStream in)
    {
        this.in = in;
    }
    public void run()
    {
        try
        {

            byte[] buf = new byte[1024];
            System.out.println("读取前、没有阻塞");
            int len = in.read(buf);
            System.out.println("读取后、阻塞结束");
            String s = new String(buf,0,len);
            
            System.out.println(s);
            
            in.close();
        }
        catch(IOException e)
        {
            System.out.println("管道读取流失败");
        }
    }
}
class Write implements Runnable
{
    private PipedOutputStream out;
    Write(PipedOutputStream out)
    {
        this.out = out;
    }
    public void run()
    {
        try
        {
            System.out.println("正在写入数据、请稍等6秒");
            Thread.sleep(6000);
            out.write("piped lai le".getBytes());
            out.close();
        }
        catch(Exception e)
        {
            throw new RuntimeException("管道输出失败");
        }
    }
}

 

IO//管道流

原文:https://www.cnblogs.com/zxl1010/p/11811726.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!