RandomAcessFile、MappedByteBuffer和缓冲读/写文件

本文探讨了在处理大文件时,通过使用MappedByteBuffer及相关类来提高读写性能的方法。实验结果显示,与传统的缓冲流相比,这种方式在效率上有显著提升。同时,文章还强调了Buffered流的BufferSize设置对性能的影响,并提供了三种不同实现方式的性能对比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目需要进行大文件的读写,调查测试的结果使我决定使用MappedByteBuffer及相关类进行文件的操作,效果不是一般的高。

网上参考资源很多,如下两篇非常不错:

1、花1K内存实现高效I/O的RandomAccessFile类
2、Java中Stream和Memory-mapped File的I/O性能对比
小结:
1、RandomAccessFile本身不带缓冲读写,和FileInputStream、FileOutputStream等一样,直接按字节读写时,性能不可接受;
2、使用MappedByteBuffer读写,固然性能会得到极大提升;其实只要自己处理缓冲,性能都会有非常大的提升,比如以下两种方式(见下记代码)中第一种使用了MappedByteBuffer,第二种自己进行缓冲,对于12M的文件,后者的效率甚至高于前者。
3、BufferedXXXX之类的缓冲流,如果仅使用默认的buffer size,性能不一定最优,要权衡不同情况各种因素设置大小。
/*
 * 测试结果与Buffer size有关
 */
// 1、使用MappedByteBuffer: 0.7s
String srcFile = "D://Noname1.txt";
String destFile = "D://copy.txt";
RandomAccessFile rafi = new RandomAccessFile(srcFile, "r");
RandomAccessFile rafo = new RandomAccessFile(destFile, "rw");
FileChannel fci = rafi.getChannel();
FileChannel fco = rafo.getChannel();
long size = fci.size();
MappedByteBuffer mbbi = fci.map(FileChannel.MapMode.READ_ONLY, 0, size);
MappedByteBuffer mbbo = fco.map(FileChannel.MapMode.READ_WRITE, 0, size);
long start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
	byte b = mbbi.get(i);
	mbbo.put(i, b);
}
fci.close();
fco.close();
rafi.close();
rafo.close();
System.out.println("Spend: " + (double) (System.currentTimeMillis() - start) / 1000 + "s");


// 2、自己处理Buffer(RandomAccessFile): 0.13s	
String srcFile = "D://Noname1.txt";
String destFile = "D://copy.txt";
RandomAccessFile rafi = new RandomAccessFile(srcFile, "r");
RandomAccessFile rafo = new RandomAccessFile(destFile, "rw");

byte[] buf = new byte[1024 * 8];

long start = System.currentTimeMillis();

int c = rafi.read(buf);

while (c > 0) {
	if (c == buf.length) {
		rafo.write(buf);
	} else {
		rafo.write(buf, 0, c);
	}

	c = rafi.read(buf);
}
rafi.close();
rafo.close();
System.out.println("Spend: " + (double) (System.currentTimeMillis() - start) / 1000 + "s");


// 3、BufferedInputStream&BufferedOutputStream: 3.02s
String srcFile = "D://Noname1.txt";
String destFile = "D://copy.txt";
FileInputStream rafi = new FileInputStream(srcFile);
FileOutputStream rafo = new FileOutputStream(destFile);

BufferedInputStream bis = new BufferedInputStream(rafi, 8192);
BufferedOutputStream bos = new BufferedOutputStream(rafo, 8192);
long size = rafi.available();

long start = System.currentTimeMillis();

for (int i = 0; i < size; i++) {
	byte b = (byte) bis.read();
	bos.write(b);
}
rafi.close();
rafo.close();
System.out.println("Spend: " + (double) (System.currentTimeMillis() - start) / 1000 + "s");


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值