任何人能解釋我的內部工作下面的代碼java.nio的內存映射的Java文件進行讀取大文件
public class MemoryMappedFileInJava {
private static int count = 10485760; //10 MB
public static void main(String[] args) throws Exception {
RandomAccessFile memoryMappedFile = new RandomAccessFile("largeFile.txt", "rw");
//Mapping a file into memory
MappedByteBuffer out = memoryMappedFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, count);
//Writing into Memory Mapped File
for (int i = 0; i < count; i++) {
out.put((byte) 'A');
}
System.out.println("Writing to Memory Mapped File is completed");
//reading from memory file in Java
for (int i = 0; i < 10 ; i++) {
System.out.print((char) out.get(i));
}
System.out.println("Reading from Memory Mapped File is completed");
}
}
我不明白幾件事情提交
1)什麼是MappedByteBuffer意味着如何它內部工作?
2)什麼是文件通道它處理的文件是需要執行的操作,即讀或寫?
3)map()方法它實際上映射的是什麼?
4)這種方法如何比使用java.io文件讀寫更快?
5)是這種方法是唯一有用的,只有當說我有400MB的堆大小,我需要讀取文件是爲8GB。或者我可以隨時使用它?
6)在上面的代碼中讀寫是逐字節的,請問怎麼解釋?