Java中通过缓冲区提高I/O系能

我们知道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();
}
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值