且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Java 中读取相对较大的字节文件的最快方法

更新时间:2023-02-19 23:24:32

我会使用一个内存映射文件,它的速度足以在同一个线程中完成.

I would use a memory mapped file which is fast enough to do in the same thread.

final FileChannel channel = new FileInputStream(fileName).getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

// when finished
channel.close();

这假设文件小于 2 GB,并且需要 10 毫秒或更短的时间.

This assumes the file is smaller than 2 GB and will take 10 milli-seconds or less.