A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 徐帅 中级黑马   /  2012-7-22 10:30  /  1537 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

管道流将输入流与输出流直接联系在一起,并且使用于多线程的情况,
那么他的作用有什么呢?我们一般在操作io流的时候不都是先从读取数据然后再写入么?
为什么管道流会先写入之后再读取,假如我想用管道流实现
一个文件的复制应该怎么实现呢,在两个类中如何实现两个文件的关联?下面的程序没有完成,
不知道这种思想是否就是错的,
请各位帮忙解答下

package cn.xushuai.base;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;


class Read implements Runnable{
        private PipedInputStream in;
        Read(PipedInputStream in){
                this.in = in;
        }
        public void run(){
                try{
                FileInputStream fis = new FileInputStream("D:\\myeclipse\\Test\\pipe.txt");
                byte[] buf = new byte[1024];
                int len = 0;
                while((len = in.read(buf))!= -1){
                }
                }
                catch(Exception e){
                        System.out.println("管道流读取失败");
                }
        }
}
class Writer implements Runnable{
        private PipedOutputStream out ;
        Writer(PipedOutputStream out){
                this.out = out;
        }
       
        public void run(){
               
                try{
                FileOutputStream bufw = new FileOutputStream("D:\\myeclipse\\Test\\pipecopy.txt");
                                     //在这里如何实现写入呢?
                }
                catch(Exception e){
                        System.out.println("管道流写入失败");
                }       
        }
       
}
public class PipedDemo {

        /**
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
                // TODO Auto-generated method stub
                PipedInputStream in = new PipedInputStream();
                PipedOutputStream out = new PipedOutputStream();
                Writer w = new Writer(out);
                  Read r = new Read(in);
                in.connect(out);
                new Thread(r).start();
                new Thread(w).start();
               
        }

}


评分

参与人数 1技术分 +1 收起 理由
韦念欣 + 1 赞一个!

查看全部评分

1 个回复

倒序浏览

回帖奖励 +2 黑马币

你的顺序有点问题,其他都没问题,主要是应该用FileInputStream嘛,你用错了。
首先是从管道的写流中把文件写入;再从管道读取流把文件读取;
管道流的写入流用FileInputStream ();而管道的读取流又用到了FileOutputStream ();
一共四个流,可能有异常,由于管道写流结束导致;但文件复制一定能完成!
  1. package cn.itcast.demo;

  2. import java.io.*;

  3. public class Piped {
  4.     public static void main(String[] args) throws IOException {
  5.         // TODO Auto-generated method stub
  6.         PipedInputStream in = new PipedInputStream();
  7.         PipedOutputStream out = new PipedOutputStream();
  8.         Writer w = new Writer(out);
  9.           Read r = new Read(in);
  10.         in.connect(out);
  11.         new Thread(r).start();
  12.         new Thread(w).start();
  13.         
  14. }

  15. }
  16. class Read implements Runnable{
  17.     private PipedInputStream in;
  18.     Read(PipedInputStream in){
  19.             this.in = in;
  20.     }
  21.     public void run(){
  22.             try{
  23.             FileOutputStream fis = new FileOutputStream("D:\\pipe2.txt");
  24.             byte[] buf = new byte[1024];
  25.             int len = 0;
  26.             while((len = in.read(buf))!= -1)
  27.             {
  28.                     fis.write(buf, 0, len);
  29.             }
  30.              fis.flush();

  31.             }
  32.             catch(Exception e){
  33.                     System.out.println("管道流读取失败"+e.getMessage());
  34.             }
  35.     }
  36. }
  37. class Writer implements Runnable{
  38.     private PipedOutputStream out ;
  39.     Writer(PipedOutputStream out){
  40.             this.out = out;
  41.     }
  42.    
  43.     public void run(){
  44.             
  45.             try{
  46.             FileInputStream fi=new FileInputStream("D:\\pipe.txt");
  47.             byte[] off=new byte[1024];
  48.             int len=0;
  49.                 while((len=fi.read(off))!=-1)
  50.                         {
  51.                         out.write(off,0,len);
  52.                         }
  53.      
  54.             
  55.             }
  56.             catch(Exception e){
  57.                     System.out.println("管道流写入失败");
  58.             }  
  59.             
  60.   
  61.     }
  62.    
  63. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
韦念欣 + 1 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马