package pc2;
//mp3的复制,通过缓冲区
//BufferedOutputStream
//BufferedInputStream
import java.io.*;
public class CopyMp3 {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
long start=System.currentTimeMillis();
copy_1();
//copy_2();
long end=System.currentTimeMillis();
System.out.println("time="+(end-start));
}
private static void copy_2() throws IOException {
// TODO Auto-generated method stub
FileInputStream fis=new FileInputStream("e:\\java_file\\music1.mp3");
FileOutputStream fos=new FileOutputStream("e:\\java_file\\music2.mp3");
int by=0;
while((by=fis.read())!=-1){
fos.write(by);
}
fis.close();
fos.close();
}
public static void copy_1()throws IOException
{
BufferedInputStream bufis=new BufferedInputStream(new FileInputStream("e:\\java_file\\music1.mp3"));
BufferedOutputStream bufos=new BufferedOutputStream(new FileOutputStream("e:\\java_file\\music2.mp3"));
int by=0;
while((by=bufis.read())!=-1)
{
bufos.write(by);
}
bufis.close();
bufos.close();
}
}
主函数调用copy_1()方法,则输出耗时7
调用copy_2(),则耗时380,
因此,使用缓冲区,加快了速度