zoukankan      html  css  js  c++  java
  • (转)四种复制文件的效率高低

    四种复制文件的效率高低

    package io2;
    
    import java.io.*;
    
    /**
     *
     * 测试复制的时间
     * Create by stefan
     * Date on 2018-05-28  10:28
     * Convertion over Configuration!
     */
    public class copy2 {
        //一个字节一个字节的复制,耗时22697毫秒
        public static  void  fun() throws IOException {
            FileInputStream fis = new FileInputStream("F:\汤包\慕课大巴\modern-java.pdf");
            FileOutputStream fos = new FileOutputStream("E:\modern-java.pdf");
            int by = 0;
            while ((by=fis.read()) != -1) {
                fos.write(by);
                fos.flush();
            }
            fis.close();
            fos.close();
        }
        //1024字节数组复制 耗时63毫秒
        public  static void  fun1() throws IOException {
            FileInputStream fis = new FileInputStream("F:\汤包\慕课大巴\modern-java.pdf");
            FileOutputStream fos = new FileOutputStream("E:\modern-java.pdf");
            int len = 0;
            byte[] bytes =new byte[1024];
            while ((len=fis.read(bytes)) != -1) {
                fos.write(bytes,0,len);
                fos.flush();
            }
            fis.close();
            fos.close();
        }
        // 一个字节一个字节复制,但是用了缓冲流 耗时64毫秒
        public static   void  fun2() throws IOException {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\modern-java.pdf"));
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\汤包\慕课大巴\modern-java.pdf"));
            int by = 0;
            while ((by=bis.read()) != -1) {
                bos.write(by);
                bos.flush();
            }
            bis.close();
            bos.close();
        }
        // 1024字节数组复制并用了缓冲流 耗时7毫秒
        public  static void  fun3() throws IOException {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\modern-java.pdf"));
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\汤包\慕课大巴\modern-java.pdf"));
            int len = 0;
            byte[] bytes =new byte[1024];
            while ((len=bis.read(bytes)) != -1) {
                bos.write(bytes,0,len);
                bos.flush();
            }
            bis.close();
            bos.close();
        }
    
        public static void main(String args[]) throws IOException {
            long t1 = System.currentTimeMillis();
            fun3();
            long t2 = System.currentTimeMillis();
            System.out.println(t2-t1);
        }
    
    }

    转自https://www.jianshu.com/p/63d1751d3eac

  • 相关阅读:
    商业软件太贵?找开源替代品
    Odoo9发行说明
    Odoo(OpenERP)配置文件openerp-server.conf详解
    MyBatis-Generator最佳实践
    elasticsearch 口水篇(1) 安装、插件
    log4j直接输出日志到flume
    Maven编译时跳过Test
    Flume1.5.0的安装、部署、简单应用(含伪分布式、与hadoop2.2.0、hbase0.96的案例)
    Flume 1.5.0简单部署试用
    一共81个,开源大数据处理工具汇总(下),包括日志收集系统/集群管理/RPC等
  • 原文地址:https://www.cnblogs.com/ZJ0065/p/10928925.html
Copyright © 2011-2022 走看看