java nio 快速read大文件

If you want to make your first example faster

FileChannel inChannel = new FileInputStream(fileName).getChannel();
ByteBuffer buffer = ByteBuffer.allocateDirect(CAPACITY); while(inChannel.read(buffer) > 0)
buffer.clear(); // do something with the data and clear/compact it. inChannel.close();
 

If you want it to be even faster.

FileChannel inChannel = new RandomAccessFile(fileName, "r").getChannel();
MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
// access the buffer as you wish.
inChannel.close();
上一篇:ctrl+alt+F1~6进入不了字符界面,黑屏的解决办法


下一篇:Alpha阶段敏捷冲刺(六)