黑马程序员技术交流社区
标题:
关于自定义字节缓冲区问题
[打印本页]
作者:
LeaonButcher
时间:
2014-7-5 22:25
标题:
关于自定义字节缓冲区问题
拷贝的图片少了100多个字节,代码如下,请大虾看看哪边有问题
import java.io.*;
class MyBufferedInputStreamDemo
{
public static void main(String[] args) throws IOException
{
MyBufferedInputStream bis = new MyBufferedInputStream(new FileInputStream("touxiang.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy2.jpg"));
int b = 0;
while((b=bis.read())!=-1){
bos.write(b);
}
bis.close();
bos.close();
}
}
class MyBufferedInputStream
{
private InputStream is;
private byte[] buf = new byte[1024];
private int pos=0,count=0;
public MyBufferedInputStream(InputStream is){
this.is = is;
}
public int read()throws IOException{
if(count ==0){
count = is.read(buf);
if(count==-1)
return -1;
pos = 0;
}
count--;
return buf[pos++]&255;
}
public void close()throws IOException{
is.close();
}
}
复制代码
作者:
xp8812
时间:
2014-7-6 00:40
兄弟,我测试了没有少字节啊
作者:
LeaonButcher
时间:
2014-7-6 08:22
xp8812 发表于 2014-7-6 00:40
兄弟,我测试了没有少字节啊
我试了好几遍都会少字节,不知道怎么回事。
作者:
xp8812
时间:
2014-7-6 13:13
LeaonButcher 发表于 2014-7-6 08:22
我试了好几遍都会少字节,不知道怎么回事。
wo图片的大小是67kb,运行完每次都能完整打开copy2图片,你的外部环境呢
作者:
╰_〃无法释怀的
时间:
2014-7-6 16:36
没有少字节
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2