java IO流的继承关系如下
InputStream
|_FileInputStream
|_BufferedInputStream
BufferedInputStream 是套在其他InputStream之外的,是用来优化里面的InputStream的性能的,FileInputStream是读文件的实现,所以通常使用BufferedInputStream&BufferedOutputStream作为FileInputStream&FileOutputStream的外包装来优化读写的性能。
下面连个例子是个人实测:

package com.myapple.utils;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class IOUtil_x {/*** 文件拷贝,不使用缓冲,一个字节一个字节读写* @param oldFile* @param newFile* @throws IOException*/public static void copyFileByByte(File oldFile,File newFile)throws IOException{if(!oldFile.exists()){throw new IllegalArgumentException("文件:"+oldFile+"不存在.");}if(!oldFile.isFile()){throw new IllegalArgumentException(oldFile+"不存在.");}FileInputStream in = new FileInputStream(oldFile);FileOutputStream out = new FileOutputStream(newFile);int c;while((c=in.read())!=-1){out.write(c);out.flush();//刷新缓冲}in.close();out.close();}/*** 文件拷贝,使用字节数组,批量读取字节* @param oldFile* @param newFile* @throws IOException*/public static void copyFileByByte_x(File oldFile,File newFile)throws IOException{if(!oldFile.exists()){throw new IllegalArgumentException("文件:"+oldFile+"不存在.");}if(!oldFile.isFile()){throw new IllegalArgumentException(oldFile+"不是文件.");}FileInputStream in = new FileInputStream(oldFile);FileOutputStream out = new FileOutputStream(newFile);byte[] bytes = new byte[8*1024];//字节数组8k大小int c;while((c=in.read(bytes,0,bytes.length))!=-1){out.write(bytes, 0, c);out.flush();//刷新缓冲}in.close();out.close();}/*** 文件拷贝,使用缓冲,一个字节一个字节读写* @param oldFile* @param newFile* @throws IOException*/public static void copyFileByBuffer(File oldFile,File newFile)throws IOException{if(!oldFile.exists()){throw new IllegalArgumentException("文件:"+oldFile+"不存在.");}if(!oldFile.isFile()){throw new IllegalArgumentException(oldFile+"不是文件.");}BufferedInputStream bis = new BufferedInputStream(new FileInputStream(oldFile));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));int c;while((c=bis.read())!=-1){bos.write(c);bos.flush();//刷新缓冲}bis.close();bos.close();}/*** 文件拷贝,使用缓冲,批量读取字节* 这是标准的IO,实际场景使用最多* @param oldFile* @param newFile* @throws IOException*/public static void copyFileByBuffer_x(File oldFile,File newFile)throws IOException{if(!oldFile.exists()){throw new IllegalArgumentException("文件:"+oldFile+"不存在.");}if(!oldFile.isFile()){throw new IllegalArgumentException(oldFile+"不是文件.");}BufferedInputStream bis = new BufferedInputStream(new FileInputStream(oldFile));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));byte[] bytes = new byte[8*1024];//字节数组8k大小int c;while((c=bis.read(bytes,0,bytes.length))!=-1){bos.write(bytes,0,c);bos.flush();//刷新缓冲}bis.close();bos.close();}
}
package com.myapple.test;import java.io.File;
import java.io.IOException;import com.myapple.utils.IOUtil_x;public class IOUtil_xTest {public static void main(String[] args) {File oldFile = new File("D:\\Eclipse\\workspaces\\mars2\\Myapple\\demo\\1.kgtemp");try {long start = System.currentTimeMillis();//文件大小:3.18 MB (3,343,152 字节) 花费时间:199037毫秒
//          IOUtil_x.copyFileByByte(oldFile, new File("D:\\Eclipse\\workspaces\\mars2\\Myapple\\demo\\a.kgtemp"));//文件大小:3.18 MB (3,343,152 字节) 花费时间:59毫秒 //字节数组由8*1024修改为100 花费时间:2117毫秒 修改为100*1024 花费时间:23毫秒IOUtil_x.copyFileByByte_x(oldFile, new File("D:\\Eclipse\\workspaces\\mars2\\Myapple\\demo\\b.kgtemp"));//文件大小:3.18 MB (3,343,152 字节) 花费时间:169485毫秒
//          IOUtil_x.copyFileByBuffer(oldFile, new File("D:\\Eclipse\\workspaces\\mars2\\Myapple\\demo\\c.kgtemp"));//文件大小:3.18 MB (3,343,152 字节) 花费时间:61毫秒 //字节数组由8*1024修改为100 花费时间:1839毫秒  修改为100*1024 花费时间:31毫秒
//          IOUtil_x.copyFileByBuffer_x(oldFile, new File("D:\\Eclipse\\workspaces\\mars2\\Myapple\\demo\\d.kgtemp"));long end = System.currentTimeMillis();System.out.println("拷贝文件话费了:" + (end - start) + "毫秒");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}

经过测试:BufferedInputStream&BufferedOutputStream在不使用字节数组时性能明显高于FileInputStream&FileOutputStream,
使用字节数组做缓冲时,当BufferedInputStream&BufferedOutputStream设置的缓冲值小于8192时,性能明显优于FileInputStream&FileOutputStream,
越是接近8192两者的性能越是接近,当大于8192时,值越大反而性能比之FileInputStream&FileOutputStream差,不过相差不大。
应该时由于BufferedInputStream&BufferedOutputStream源码设计时默认缓冲区大小是8192字节导致,至于更深入的分析性能差异还望高手指点。
总结:一般情况下使用BufferedInputStream&BufferedOutputStream优化提高读写性能。

FileInputStreamFileOutputStream 和 BufferedInputStreamBufferedOutputStrem的性能测试小案例相关推荐

  1. Python:通过一个小案例深入理解IO多路复用

    通过一个小案例深入理解IO多路复用 假如我们现在有这样一个普通的需求,写一个简单的爬虫来爬取校花网的主页 import requests import timestart = time.time()u ...

  2. iptables小案例,nat表应用

    2019独角兽企业重金招聘Python工程师标准>>> iptables小案例: 需求1: 只针对filter表,预设INPUT链DROP,其他两个链ACCEPT,然后针对192.1 ...

  3. 4.10/4.11/4.12 lvm讲解 4.13 磁盘故障小案例

    4.10/4.11/4.12 lvm讲解 操作流程: 磁盘分区-->创建物理卷-->划分为卷组-->划分成逻辑卷-->格式化.挂载-->扩容. 磁盘分区 注: 创建分区时 ...

  4. python程序实例教程基础-编程小案例

    编程小案例 本小节实现一个通讯录管理程序,通过这个案例来融会贯通之前所学习的知识,该程序使用到如下知识点: 条件选择 循环 列表 字典 键盘输入 屏幕输出 编写程序 addr-manage.py 实现 ...

  5. axios vue 回调函数_Vue 02 —— Vue 入门小案例~使用 Axios 中的GET、POST请求

    作为后端攻城狮,写前端代码是一种什么体验? 相信不少人和 @Python大星 一样,有写过前端代码的经历. 记录一下,Vue 框架开发中"啼笑皆非"的故事,非专业前端人员,该案例无 ...

  6. 【Node.js学习小案例】DNS域名解析 一

    Node.js 百度百科: Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台, 用来方便地搭建快速的 易于扩展的网络应用· Node.js 借助事件驱动, 非阻塞I/ ...

  7. linux下set和eval的使用小案例精彩解答

    linux下set和eval的使用小案例解答 本博文主要是讲解学生提出的如下一行命令脚本定义的真正内涵: runlevel=$(set -- $(runlevel); eval "echo  ...

  8. shell讲解-小案例

    shell讲解-小案例 一.文件拷贝输出检查 下面测试文件拷贝是否正常,如果cp命令并没有拷贝文件myfile到myfile.bak,则打印错误信息.注意错误信息中basename $0打印脚本名.如 ...

  9. 函数作为返回值练习 作用域和作用域链及预解析 闭包 闭包小案例

    函数作为返回值练习 <!DOCTYPE html> <html lang="en"> <head><meta charset=" ...

  10. 使用idea编写SparkStreaming消费kafka中的数据,并对数据进行某一指标的处理【小案例】(五)

    接    使用idea编写SparkStreaming消费kafka中的数据[小案例](四) https://georgedage.blog.csdn.net/article/details/1035 ...

最新文章

  1. 余承东安卓鸿蒙,鸿蒙亮点多,网友嗨了!华为P50还没定,余承东:最强大操作系统...
  2. 不要再被 Python 洗脑了!!
  3. python能做软件吗-python能够做软件的自动化测试吗?
  4. mysql下count(*)和count(1)的区别
  5. How should I set up tag files for a multi-level directory hierarchy? kiss snow
  6. ITK:自适应直方图均衡图像滤波器
  7. RedHat Enterprise Linux之raid5磁盘阵列
  8. mysql Error Code: 1005(errorno:121)解决
  9. C#中不常见的运算符功能汇总
  10. 学生上课睡觉班主任怎么处理_【师问师答】学生上课说话,点名批评还嘴怎么办?...
  11. leetcode 236. 二叉树的最近公共祖先 思考分析
  12. [js] 写一个获取页面中所有checkbox的方法
  13. 罕有数据库系统之比拟 - Oracle数据库
  14. 查看linux系统端口占用情况
  15. 网站中引入百度地图的方法分享(含源码)
  16. 西门子PLC的选型方法总结
  17. 2022泰迪杯数据挖掘挑战赛C题方案及赛后总结:疫情背景下的周边游需求图谱分析
  18. Java如何生成彩色二维码(利用zxing包)
  19. 移动端 touch 手机拖动 css停止问题
  20. 海洋地球物理探测方法综述(一)地震和重磁

热门文章

  1. Flutter 未检测到iOS模拟器以及Android Studio无法获取iOS模拟器的问题
  2. MPB:林科院袁志林组-树木共生真菌菌株纯化及快速鉴定方法
  3. 【VRP问题】基于模拟退火算法求解带时间窗的车辆路径规划问题VRPTW
  4. 从Excel中随机取出几行
  5. VS2017 社区版 许可证过期
  6. 游程编码解密(C语言详解)
  7. 14. 接口隔离模式之Facade模式(门面模式/外观模式)
  8. ftp服务器和文件夹共享文件夹,ftp服务器和文件夹共享文件夹
  9. 成功破解某app加密接口
  10. 电商后台管理系统项目实例