第四周JAVA学习笔记(四)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile {
    /**
     * @param args
     */
    public static void copy(File a, File b){
        try {
            FileInputStream fis = new FileInputStream ("C:\\Users\\think\\Desktop\\Java作业4\\a.mp3");
            FileOutputStream fos = new FileOutputStream ("C:\\Users\\think\\Desktop\\Java作业4\\c.mp3");
            
            byte[] buf = new byte[2048];
            int len;
            int read = fis.read();
            while((len = fis.read(buf))!=-1){
            fos.write(buf, 0, len);
            }
                    
            fis.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
    
        
        File a = new File("");
        File b = new File("");
        long start,end;
        start = System.currentTimeMillis();
        copy(a,b);
        end = System.currentTimeMillis();
        System.out.println("用时:" + (end - start) + "ms");
    }
}

添加一个缓冲流来减少时间的运算

byte[] buf = new byte[2048];
int len;
int read = fis.read();
while((len = fis.read(buf))!=-1){
fos.write(buf, 0, len);
}

再新建一个文件夹进行时间的计算

File a = new File("");
File b = new File("");
long start,end;
start = System.currentTimeMillis();
copy(a,b);
end = System.currentTimeMillis();
System.out.println("用时:" + (end - start) + "ms");

posted on 2016-04-08 18:12  上厕所烫屁股  阅读(218)  评论(0)    收藏  举报