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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

LeaonButcher

初级黑马

  • 黑马币:32

  • 帖子:15

  • 精华:0

© LeaonButcher 初级黑马   /  2014-7-5 22:25  /  763 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

拷贝的图片少了100多个字节,代码如下,请大虾看看哪边有问题

  1. import java.io.*;

  2. class MyBufferedInputStreamDemo
  3. {
  4.         public static void main(String[] args) throws IOException
  5.         {
  6.                 MyBufferedInputStream bis = new MyBufferedInputStream(new FileInputStream("touxiang.jpg"));
  7.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy2.jpg"));
  8.                 int b = 0;
  9.                 while((b=bis.read())!=-1){
  10.                         bos.write(b);
  11.                 }
  12.                 bis.close();
  13.                 bos.close();
  14.         }
  15. }

  16. class MyBufferedInputStream
  17. {
  18.         private InputStream is;
  19.         private byte[] buf = new byte[1024];
  20.         private int pos=0,count=0;

  21.         public MyBufferedInputStream(InputStream is){
  22.                 this.is = is;
  23.         }

  24.         public int read()throws IOException{
  25.                 if(count ==0){
  26.                         count = is.read(buf);
  27.                         if(count==-1)
  28.                                 return -1;
  29.                         pos = 0;
  30.                 }
  31.                 count--;
  32.                 return buf[pos++]&255;
  33.         }

  34.         public void close()throws IOException{
  35.                 is.close();
  36.         }

  37. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
李小然 + 1

查看全部评分

4 个回复

倒序浏览
兄弟,我测试了没有少字节啊
回复 使用道具 举报
xp8812 发表于 2014-7-6 00:40
兄弟,我测试了没有少字节啊

我试了好几遍都会少字节,不知道怎么回事。
回复 使用道具 举报
LeaonButcher 发表于 2014-7-6 08:22
我试了好几遍都会少字节,不知道怎么回事。

wo图片的大小是67kb,运行完每次都能完整打开copy2图片,你的外部环境呢

评分

参与人数 1技术分 +1 收起 理由
李小然 + 1

查看全部评分

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