zoukankan      html  css  js  c++  java
  • java FileI(O)nputStream为什么比BufferedI(O)utputStream慢?

    因为buffered多了一个缓冲区,读和写都是先把硬盘或者内存中的数据放到内存中一块缓存区域,到一定大小读写到硬盘或者内存

     

    package io;

    import java.io.*;

    public class FileIOTest {

        /**
         * @param args
         * @throws FileNotFoundException
         */
        public static void main(String[] args) throws Exception {

            //有buff的File***Stream
            System.out.println("有buff的File***Stream耗时:");
            new TimeTest() {
                void run()throws Exception{
                    FileInputStream fis = new FileInputStream("F:\Test\file.zip");

                    FileOutputStream fos = new FileOutputStream("F:\Test\file1.zip");

                    byte[] buf = new byte[1024];

                    int length = 0;


                    while ((length = fis.read(buf)) > 0) {
                        fos.write(buf, 0, length);
                    }

                    fis.close();
                    fos.close();

                }
            }.getTime();
           
            //有buff的Buffered***Stream
            System.out.println("有buff的Buffered***Stream耗时:");
            new TimeTest() {
                void run()throws Exception{

                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\Test\file.zip"));

                    BufferedOutputStream bos = new BufferedOutputStream(
                            new FileOutputStream("F:\Test\file2.zip"));
                   
                    byte[] buf = new byte[1024];

                    int length = 0;
                   
                    while ((length = bis.read(buf)) > 0) {
                        bos.write(buf, 0, length);
                    }

                    bis.close();
                    bos.close();
                   
                }
            }.getTime();
           
            //无buff的File***Stream
            System.out.println("无buff的File***Stream耗时:");
            new TimeTest() {
                void run()throws Exception{
                    FileInputStream fis = new FileInputStream("F:\Test\file.zip");

                    FileOutputStream fos = new FileOutputStream("F:\Test\file3.zip");

                    int data = 0;

                    while ((data = fis.read()) !=-1) {
                        fos.write(data);
                    }

                    fis.close();
                    fos.close();
                }
            }.getTime();
           
            //无buff的Buffered***Stream
            System.out.println("无buff的Buffered***Stream耗时:");
            new TimeTest() {
                void run()throws Exception{
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\Test\file.zip"));

                    BufferedOutputStream bos = new BufferedOutputStream(
                            new FileOutputStream("F:\Test\file4.zip"));
                   
                    int data = 0;
                   
                    int i =bis.available();
                   
                    while ((data = bis.read()) !=-1) {
                        bos.write((byte)data);
                    }

                    bis.close();
                    bos.close();
                }
            }.getTime();
        }

    }

    //抽象的不太好的模板设计模式
    abstract class TimeTest {

        void getTime() {
            long start = System.currentTimeMillis();
            try {
                run();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(System.currentTimeMillis() - start);
        }

        abstract void run() throws Exception;
    }

     

     

    测试数据248kb

     

    测试结果:

     

    有buff的File***Stream耗时:
    8
    有buff的Buffered***Stream耗时:
    2
    无buff的File***Stream耗时:
    1369
    无buff的Buffered***Stream耗时:
    14

  • 相关阅读:
    javascript定义
    JavaScript学习笔记
    PostgreSQL数据库配置
    python 时间戳转时间 timestamp to time
    WebGIS:Vue+Flask+PostGIS+Geoserver开发环境配置
    GeoServer环境配置
    Vue前端环境配置
    Flask后端环境配置
    1.顺序表
    汇编语法--AT&T VS. intel
  • 原文地址:https://www.cnblogs.com/flying607/p/3460389.html
Copyright © 2011-2022 走看看