zoukankan      html  css  js  c++  java
  • 使用拷贝文件测试(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拷贝有时候反而比不用缓存拷贝还慢

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

  • 相关阅读:
    Unix进程和线程管理及其异同
    UnixIPC之共享内存
    Unix/Linux常用文件操作
    java中int和Integer比较
    JAVA四种引用类型
    JAVA-Exception&Error
    JAVA特性-跨平台/面向对象
    JAVA单向链表实现
    linux安装及配置c++的opencv库
    static_cast、const_cast、dynamic_cast、reinterpret_cast
  • 原文地址:https://www.cnblogs.com/tc310/p/9648157.html
Copyright © 2011-2022 走看看