本帖最后由 黑马-王双 于 2013-5-6 16:25 编辑
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
{
//通过in对象读取硬盘数据,存入buf中
if(count==0)
{
count =in.read(buf);
if(count<0)
return -1;
pos=0;
byte b=buf[pos];
count--;
pos++;
return b;
}
else if(count>0)
{
byte b=buf[pos];
count--;
pos++;
return b;
}
return -1;
}
public void myClose()throws IOException
{
in.close();
}
}
class CopyMp3
{
public static void main(String[] args) throws IOException
{
long start=System.currentTimeMillis();
copy_2();
long end=System.currentTimeMillis();
System.out.println((end-start)+”毫秒”);
}
public static void copy_2() throws IOException
{
MyBufferedInputStream bufis=new MyBufferedInputStream(new FileInputStream(”1.mp3“));
BufferedOutputStream bufos=new BufferedOutputStream(new FileOutputStream(”1_copy.mp3“));
int ch=0;
while((ch=bufis.myRead())!=-1)
{
bufos.write(ch);
}
bufos.close();
bufis.myClose();
}
} |
|