黑马程序员技术交流社区

标题: 自定义字节缓冲流复制文件问题 [打印本页]

作者: 于启会    时间: 2012-9-9 23:14
标题: 自定义字节缓冲流复制文件问题
本帖最后由 于启会 于 2012-9-10 00:27 编辑

  1. import java.io.*;
  2. public class MyBufferedInputStream{
  3.         private InputStream in;
  4.         private byte[] buf=new byte[1024];  //定义一个byte类型数组。
  5.         private int pos=0,count=0;  //定义一个下标器,一个计数器
  6.         public MyBufferedInputStream(InputStream in){
  7.                 this.in=in;
  8.         }
  9.         public int MyReadLine() throws IOException{
  10.                
  11.                 if(count==0){                        
  12.                         count=in.read(buf);
  13.                         
  14.                         if(count<0){
  15.                                 pos=0;
  16.                                 return -1;
  17.                         }        
  18.                         byte b=buf[pos];
  19.                         count--;
  20.                         pos++;
  21.                         return b&255;
  22.                 }
  23.                 else if(count>0){
  24.                         byte b =buf[pos];
  25.                         count--;
  26.                         pos++;
  27.                         return b&255;
  28.                 }
  29.                 return -1;
  30.         }
  31.         public void Myclose() throws IOException{
  32.                 this.in.close();
  33.         }
  34. }




  35. import java.io.*;
  36. public class BufferedStreamDemo{
  37. public static void main(String arg[]){
  38.   try{
  39.    FileInputStream fis = new FileInputStream("E:\\<a href="file://\\IO">IO</a>流<a href="file://\\music\\">\\music\\</a>柴米油盐酱醋茶.mp3");
  40.    FileOutputStream fos=new FileOutputStream("E:\\<a href="file://\\IO">IO</a>流\\哈哈.mp3");
  41.    copy_1(fis,fos);
  42.   }catch(IOException e){
  43.    System.out.println(e.toString());
  44.   }
  45. }
  46. public static void copy_1(InputStream input,OutputStream out){
  47.   MyBufferedInputStream bis=new MyBufferedInputStream(input);
  48.   BufferedOutputStream bos=new BufferedOutputStream(out);
  49.   try{
  50.    int temp=0;
  51.    while((temp=bis.MyReadLine())!=-1){
  52.     bos.write(temp);
  53.    }
  54.   }catch(IOException e){
  55.    System.out.println(e.toString());
  56.   }finally{
  57.    try{
  58.     if(bos!=null){
  59.      bos.close();
  60.     }
  61.     if(bis!=null){
  62.      bis.Myclose();
  63.     }
  64.    }catch(IOException e){
  65.     System.out.println(e.toString());
  66.    }
  67.   }
  68. }
  69. }

  70. 运行能复制,但是复制的文件大小比原文件大小很多,到底是哪里出了错?
复制代码

作者: 李菁    时间: 2012-9-9 23:29
public int MyReadLine() throws IOException{

11.               

12.                if(count==0){                        

13.                        count=in.read(buf);

14.                        

15.                        if(count<0){

16.                                pos=0;  把这句话放到if外边,if是判断是否取到最后了。
                                            pos=0;这句话代表每次往里面重装数据,pos这个指针都要归0


17.                                return -1;

18.                        }        

19.                        byte b=buf[pos];

20.                        count--;

21.                        pos++;

22.                        return b&255;

23.                }

24.                else if(count>0){

25.                        byte b =buf[pos];

26.                        count--;

27.                        pos++;

28.                        return b&255;

29.                }

30.                return -1;

31.        }






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2