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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 于启会 中级黑马   /  2012-9-9 23:14  /  1448 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 于启会 于 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. 运行能复制,但是复制的文件大小比原文件大小很多,到底是哪里出了错?
复制代码

评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1 赞一个!

查看全部评分

2 个回复

倒序浏览
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.        }

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