码迷,mamicode.com
首页 > 其他好文 > 详细

IO流 管道流

时间:2016-01-24 16:47:29      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

package com.yyq;
import java.io.*;
/*
 * 管道流
 * RandomAccessFile
 * 随机访问文件,自身具备读写的方法
 * 通过 SkipBytes(int x)seek(int  x)
 * 结合多线程技术  管道流
 * properties  io+集合相结合
 */
class Read implements Runnable{
    private PipedInputStream in;
    Read(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){
            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();
        
    }

}

 

IO流 管道流

标签:

原文地址:http://www.cnblogs.com/yangyongqian/p/5155267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!