對1.09G文件操作時間,談及個人理解
public static void copyFlie(File srcFile ,File destFile) throws IOException {
if(!srcFile.exists()) {
throw new IllegalArgumentException("目標(biāo)文件:"+srcFile +" 不存在");
}
if(!srcFile.isFile()) {
throw new IllegalArgumentException(srcFile+" 不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
byte[] buf = new byte[8*1024];
int b;
while((b = in.read(buf, 0, buf.length))!=-1) {
out.write(buf, 0, b);
}
in.close();
out.close();
}
/**
* 進(jìn)行文件的拷貝,利用帶緩存的字節(jié)流
*?
* @param srcFile
* @param destFile
*/
public static void copyByBuffer(File srcFile ,File destFile) throws IOException{
if(!srcFile.exists()) {
throw new IllegalArgumentException("目標(biāo)文件:"+srcFile +" 不存在");
}
if(!srcFile.isFile()) {
throw new IllegalArgumentException(srcFile+" 不是文件");
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] buf = new byte[8*102];
int c;
while((c=bis.read(buf , 0 ,buf.length))!=-1) {
bos.write(buf,0,c);
bos.flush();//刷新緩沖區(qū)
}
bis.close();
bos.close();
}
對1.09G文件的操作,進(jìn)行五組數(shù)據(jù)對比,copyFlie平均用時14093,copyByBuffer當(dāng)flush在循環(huán)外平均用時13971
由于每次讀取時間不確定,有的copyfile快于copybybuffere,有的相反,所以感覺速度不相上下。當(dāng)copybybuffere采用單字節(jié)的時候,時間不可想象,太長了,如果flush在循環(huán)內(nèi)部時間也會過長,沒有做對比
2019-07-08
這是為啥。。