Java中通过缓冲区提高I/O性能
来源:互联网 发布:有什么特效软件 编辑:程序博客网 时间:2024/05/16 06:21
我们知道Java中一般的输入输出流类都是用单字节的读取方法来进行I/O操作的,也就是说每次只读写一个字节的数据,这种方法显然繁琐低效。如果从设备读取10M的文件,每次读取一个字节,完成操作将需要做10M/次I/O操作,I/O操作又是一件相当耗时的事情,无疑在很大程度上降低了系统的性能。
Java中专门提供提高I/O效率的缓冲类,这好比在数据读写时提供一个临时缓冲区,每次读取一个缓冲区大小的数据,将这数据库一次性写入目标设备。下图中分别为两种读取方式。
举个简单例子,在A地有10000本书需要搬到B地,如果一次搬1本,需要10000次。如果每次取1000本放到一个货车上,运到B地,需要10次完成。货车相当于是缓存区。同样道理,开设一个数据缓存区每次读取一数据块对于提高读取效率有显著提升。下面用一个具体代码示例来表示二者的性能差别。
Java代码
import java.io.*; /******************************************************************************* * * @author pengcqu * */ public class TestBuffer { public static void main(String args[]) throws IOException { TestBuffer br = new TestBuffer(); String from = "d:/a/1.MP3"; long startTime = System.currentTimeMillis(); br.readWrite(from,"d:/b/2.MP3"); long endTime = System.currentTimeMillis(); System.out.println("直接读取耗时:" + (endTime - startTime) +"ms"); long startTime1 = System.currentTimeMillis(); br.readWriteWithBuffer(from, "d:/b/3.MP3"); long endTime1 = System.currentTimeMillis(); System.out.println("使用缓冲区读取耗时:" + (endTime1 - startTime1) +"ms"); } /*************************************************************************** * 直接读取文件 * * @param from * @param to * @throws IOException */ public static void readWrite(String from, String to) throws IOException { InputStream in = null; OutputStream out = null; try { in = new FileInputStream(from); out = new FileOutputStream(to); while (true) { int data = in.read(); if (data == -1) { break; } out.write(data); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } /*************************************************************************** * 使用缓存区读写文件 * @param from * @param to * @throws IOException */ public static void readWriteWithBuffer(String from, String to) throws IOException { InputStream inBuffer = null; OutputStream outBuffer = null; try { inBuffer = new BufferedInputStream(new FileInputStream(from)); outBuffer = new BufferedOutputStream(new FileOutputStream(to)); while (true) { int data = inBuffer.read(); if (data == -1) { break; } outBuffer.write(data); } } finally { if (inBuffer != null) { inBuffer.close(); } if (outBuffer != null) { outBuffer.close(); } } } }
结果:对于5.8M大小的文件,直接读写耗时:39266ms;使用缓冲区读写耗时:719ms。可见通过Buffer读写文件性能的明显优于直接读写。
如有理解不妥之处,请各位高手补充意见。
0 0
- java通过缓冲区提高I/O性能
- Java中通过缓冲区提高I/O性能
- Java中通过缓冲区提高I/O系能
- Java I/O关于缓冲区部分提高性能的源码分析【Stream】
- 调整(提高) Java I/O 性能
- 使用异步 I/O 提高程序性能
- 增加db_block_size能否提高I/O性能?
- LVM条带化提高I/O性能
- Java Tip 26: How to improve Java's I/O performance-如何提高java I/O性能
- 调整 Java I/O 性能
- 调整 Java I/O 性能
- 调整 Java I/O 性能
- 调整 Java I/O 性能
- 调整 Java I/O 性能
- Java I/O性能提升
- 调整 Java I/O 性能
- 关于I/O缓冲区
- I/O缓冲区
- java http请求
- JSF学习六 空格
- 宣布发布长期保留 Azure Backup功能
- android高薪之路--笔试面试题汇总
- K-mean算法的优点缺点
- Java中通过缓冲区提高I/O性能
- TCP 三次握手 四次挥手
- Python中的dict
- 第十四周项目1折腾二维数组
- 深入浅出单实例Singleton设计模式
- html+javascript实现广告窗自由浮动
- 数据结构复习第一遍
- 四、创建型模式:建造者模式(BUILDER)
- 远程服务让服务不留死角