• 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

  • 相关阅读:
    极客互动极客技术专题【003期】:java mvc 增删改查 自动生成工具来袭
    协议命令网络工程试验一
    主题网站分享两套免费的超棒响应式HTML5网站模板
    算法结点图的多源点最短路问题和传递闭包之FloydWarshall算法 By ACReaper
    属性页面Flexbox布局的简单演示之二
    数据库性能Quest Performance Analysis Overview
    网站查看帮助查看本地表单元素样子的网站 Native Form Elements
    文件格式配置文件weka频繁模式挖掘使用方法
    风格希望分享8个超棒的免费界面UI设计
    方法事务applicationContext.xml
  • 原文地址:https://www.cnblogs.com/flying607/p/3460389.html
Copyright © 2020-2023  润新知