本帖最后由 魏亮 于 2012-10-15 10:27 编辑
import java.io.*;
class MyBufferedInputStreamDemo
{
public static void main(String[] args) throws IOException
{
MyBufferedInputStream mybufis = new MyBufferedInputStream(new FileInputStream("1.mp3"));
//BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("1.mp3"));
BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("2.mp3"));
int ch=0;
while ((ch=mybufis.myRead())!=-1)
{
bufos.write(ch);
}
mybufis.myClose();
bufos.close();
}
}
class MyBufferedInputStream
{
private InputStream is;
MyBufferedInputStream(InputStream is)
{
this.is=is;
}
private int count=0,pos=0;
private byte[] buf = new byte[1024];
public int myRead() throws IOException
{
if (count==0)
{
count=is.read(buf);
pos=0;
if (count<0)
{
return -1;
}
/*
byte b = buf[pos];
pos++;
count--;
return b&255;
*/去掉这上边这个红色的代码的话复制的文件不能用,写上的话正常(写上后是老师的代码),为什么呢?我自己写的代码,也就是去掉上面红色代码后的代码,没有什么错误呀,就是没有立刻返回数组里的值而已呀,为什么不行??想不通呀!
}
else if (count>0)
{
byte b = buf[pos];
pos++;
count--;
return b&255;
}
return -1;
}
public void myClose() throws IOException
{
is.close();
}
}
|