/*
需求
自定义字节流缓冲区
*/
import java.io.*;
class MyBufferedInputStream
{
private InputStream in;
private byte[] buf = new byte[1024*4];
private int pos = 0,count = 0;
MyBufferedInputStream(InputStream in)
{
this.in = in;
}
//一次读一个字符,从缓冲区(字节数组)获取
public int myRead()throws IOException
{
//返回为int类型是为了提升返回值的类型
//通过in对象读取硬盘上数据,并储存buf中
if (count==0)
{
count = in.read(buf);
if(count<0)
return -1;
pos = 0;
byte b = buf[pos];
count--;
pos++;
return b&255;
}
else
{
byte b = buf[pos];
count--;
pos++;
return b&0xff;
}
}
public void myClose()throws IOException
{
in.close();
}
}
class MyBufferedInputStreamDemo
{
public static void main(String[] args)throws IOException
{
myCopyMusic();
}
public static void myCopyMusic()throws IOException
{
MyBufferedInputStream mbis = new MyBufferedInputStream(new FileInputStream("F:\\2.mp3"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\1.mp3"));
int ch = 0;
while((ch=mbis.myRead())!=-1)
{
bos.write(ch);
}
bos.close();
mbis.myClose();
}
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。