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

转载于:https://www.cnblogs.com/flying607/p/3460389.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值