zoukankan      html  css  js  c++  java
  • 字节流的三种操作方法效率对比

    public class IOStream {
    public static void main(String[] args) throws IOException {
    //目标文件大小:43.9M
    Original ();//使用自建缓冲区方式复制文件耗时148毫秒
    Buff (); //使用包装流复制文件耗时2727毫秒
    Utils ();//使用工具类复制文件耗时104毫秒
    }
    //使用自建缓冲区方式复制文件
    private static void Original() throws IOException {

    File file = new File ("source.mp4");
    InputStream in = new FileInputStream (file);
    OutputStream out = new FileOutputStream ("/Users/Shared/a.mp4");

    long startTime = System.currentTimeMillis ();

    byte[] arr = new byte[8192];
    int len;
    while ((len = in.read (arr) )!= -1){
    out.write (arr);
    }

    long endTime = System.currentTimeMillis ();
    System.out.println (endTime-startTime);

    in.close ();
    out.close ();
    }
    //使用包装流方式复制文件
    private static void Buff() throws IOException {
    File file = new File ("source.mp4");
    InputStream in = new FileInputStream (file);
    BufferedInputStream bfi= new BufferedInputStream (in);
    OutputStream out = new FileOutputStream ("/Users/Shared/b.mp4");
    BufferedOutputStream bfo = new BufferedOutputStream (out);

    long startTime = System.currentTimeMillis ();

    int len;
    while ((len = bfi.read () )!= -1){
    bfo.write (len);
    }

    long endTime = System.currentTimeMillis ();
    System.out.println (endTime-startTime);

    bfi.close ();
    bfo.close ();
    }

    //使用工具包复制文件
    private static void Utils() throws IOException {

    File file1 = new File ("source.mp4");//文件大小:43.9M
    File file2 = new File ("/Users/Shared/c.mp4");

    long startTime = System.currentTimeMillis ();

    FileUtils.copyFile (file1,file2);

    long endTime = System.currentTimeMillis ();
    System.out.println (endTime-startTime);
    }
    }
  • 相关阅读:
    正则表达式
    数组去重
    [WOJ4354] 蜀石经
    [NOI2002] 银河英雄传说
    [洛谷P2186] 小Z的栈函数
    [洛谷P2756]飞行员配对方案问题
    [洛谷P2071] 座位安排
    [洛谷P2417]课程
    [洛谷P1640] [SCOI2010]连续攻击游戏
    [洛谷P3512 [POI2010]PIL-Pilots]
  • 原文地址:https://www.cnblogs.com/gdwkong/p/7794427.html
Copyright © 2011-2022 走看看