使用拷贝文件测试(BufferedInputStream,FileInputStream)


package
com.demo; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class CopyFileDemo { public static void main(String[] args) { long beginTime = System.currentTimeMillis(); copyFileBuffer(); long endTime = System.currentTimeMillis(); System.out.println("Buffered拷贝所花的时间:"+(endTime-beginTime)+"毫秒"); long beginTime1 = System.currentTimeMillis(); copyFile(); long endTime1 = System.currentTimeMillis(); System.out.println("FileIn/Out拷贝所花的时间:"+(endTime1-beginTime1)+"毫秒"); } public static void copyFileBuffer(){ // File file= new File("G:/MP4视频"); // file.mkdir(); try { BufferedInputStream bin = new BufferedInputStream(new FileInputStream("G:/install/ISO/XLJS_GHOST_XP_SP3_FT_2.0.iso")); BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("H:/TEST/1.iso")); byte[] bt = new byte[8192]; int n=0; while ((n=bin.read(bt))!=-1) { bout.write(bt, 0, n); } bout.flush(); bin.close(); bout.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void copyFile(){ try { FileInputStream fin = new FileInputStream("G:/install/ISO/XLJS_GHOST_XP_SP3_FT_2.0.iso"); FileOutputStream fout=new FileOutputStream("H:/TEST/2.iso"); byte[] by = new byte[8192]; int len = 0; while ((len=fin.read(by))!=-1) { fout.write(by, 0, len); } fout.flush(); fout.close(); fin.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

测试结果:文件大小:700M,用Buffered拷贝有时候反而比不用缓存拷贝还慢

有时候还是使用缓存快点,但是不是特别明显

 

posted @ 2018-09-14 18:05  tc310  阅读(225)  评论(0编辑  收藏  举报