zoukankan      html  css  js  c++  java
  • Java IO流之【缓冲流和文件流复制文件对比】

    与文件流相比,缓冲流复制文件更快

    代码:

    package Homework;
    
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    
    /**
     * 1 )将"今年是反法西斯胜利70周年,举国欢庆,所以要放假啦" 字符串
     * 使用文件字符输出流 写入到oldhappy.dt文件中,复写10000行,
     * 要求换行 在文件的开头写入当前的时间 精确到毫秒在文件的结尾也写入当前的时间 精确到毫秒。
     * @author Administrator
     *
     */
    public class Test1 {
    
        public static void main(String[] args) {
            copy1();
            copy2();
        }
    
        //使用文件流复制文件
        public static void copy1(){
            FileOutputStream fos=null;
            try {
                //创建输入流,并创建要写入的文件oldhappy.dt
                fos=new FileOutputStream(new File("oldhapy.dt"));
                //获取写入前的当前时间
                long l=System.currentTimeMillis();
                //格式化时间
                SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
                String time=s.format(l);
                //写入时间
                fos.write((time+"
    ").getBytes());
                //写入要写的内容,并换行 
                //   
    表示换行
                for(int i=1;i<=10000;i++){
                    fos.write("今年是反法西斯胜利70周年,举国欢庆,所以要放假啦
    ".getBytes());
                    fos.flush();
                }
                //获取写完后 时间
                long l2=System.currentTimeMillis();
                String time2=s.format(l2);
                fos.write(time2.getBytes());
    
                //复制文件的时间
                System.out.println("使用文件流复制文件时间:"+(l2-l));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                if(fos!=null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    
        //使用缓冲流复制文件
        public static void copy2(){
            BufferedOutputStream bos=null;
    
            try {
                bos=new BufferedOutputStream(new FileOutputStream("newhapy.dt"));
    
                long l=System.currentTimeMillis();
                //格式化时间
                SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
                String time=s.format(l);
                //写入时间
                bos.write((time+"
    ").getBytes());
                //写入要写的内容,并换行 
                //   
    表示换行
                for(int i=1;i<=10000;i++){
                    bos.write("今年是反法西斯胜利70周年,举国欢庆,所以要放假啦
    ".getBytes());
                    bos.flush();
                }
                //获取写完后 时间
                long l2=System.currentTimeMillis();
                String time2=s.format(l2);
                bos.write(time2.getBytes());
    
                //复制文件的时间
                System.out.println("使用缓冲流复制文件时间:"+(l2-l));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                if(bos!=null){
                    try {
                        bos.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    
    
    }
    

    运行结果比较:

    这里写图片描述

  • 相关阅读:
    windows下Yarn安装与使用(两种方法)
    git配置公钥---解决码云出现git@gitee.com: Permission denied (publickey)
    npm使用国内镜像的两种方法
    【LeetCode】33. Search in Rotated Sorted Array (4 solutions)
    【LeetCode】83. Remove Duplicates from Sorted List
    【LeetCode】82. Remove Duplicates from Sorted List II
    【LeetCode】85. Maximal Rectangle
    【LeetCode】84. Largest Rectangle in Histogram
    【LeetCode】87. Scramble String
    【LeetCode】162. Find Peak Element (3 solutions)
  • 原文地址:https://www.cnblogs.com/TCB-Java/p/6797632.html
Copyright © 2011-2022 走看看