FileChannel 的一个最好的功能就是能将文件的某个部分直接映射到内存。这要感谢 FileChannel.map() 方法,这个方法有下面三个参数:
mode:映射到内存需要指定下面三种模式之一:MapMode.READ_ONLY (只读映射;如果试图写入,就会抛出 ReadOnlyBufferException),MapMode.READ_WRITE (读/写 映射;改变结果缓存将会传播到文件,并且映射到相同文件的其它程序也会被影响)MapMode.PRIVATE(私有映射;改变结果缓存不会传播到文件, 并且映射到相同文件的其它程序也不会被影响)。
position;映射在文件中的开始位置(不能为负数)。
size:映射区域的大小(0 ≤ size ≤ Integer.MAX_VALUE)。
注意,只有在打开 channel 的时候设置为 read 打开选项才能使用 read-only 模式,只有在打开 channel 时候设置 read 和 write 打开选项才能设置 read_write 和 private 模式。
map() 方法将会返回 MappedByteBuffer,它表示提取的文件区域。这个类继承自 ByteBuffer 并且还提供了下面三个方法:
force():将缓冲区的改变传播到文件。
load():从物理内存加载缓冲内容
isLoaded():判断缓冲内容是否在物理内存中
下面的代码将从 C:\rafaelnadal\tournaments\2009\BrasilOpen.txt 获得一个新的 channel,并使用 READ_ONLY 的模式映射整个文件内容,为了验证程序是否成功,将打印出字节缓冲的内容:
importjava.io.IOException;importjava.nio.CharBuffer;importjava.nio.MappedByteBuffer;importjava.nio.channels.FileChannel;importjava.nio.charset.CharacterCodingException;importjava.nio.charset.Charset;importjava.nio.charset.CharsetDecoder;importjava.nio.file.Path;importjava.nio.file.Paths;importjava.nio.file.StandardOpenOption;importjava.util.EnumSet;public classMain {public static voidmain(String[] args) {
Path path= Paths.get("C:/rafaelnadal/tournaments/2009", "BrasilOpen.txt");
MappedByteBuffer buffer= null;try (FileChannel fileChannel =(FileChannel.open(path,
EnumSet.of(StandardOpenOption.READ)))) {
buffer= fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
}catch(IOException ex) {
System.err.println(ex);
}if (buffer != null) {try{
Charset charset=Charset.defaultCharset();
CharsetDecoder decoder=charset.newDecoder();
CharBuffer charBuffer=decoder.decode(buffer);
String content=charBuffer.toString();
System.out.println(content);
buffer.clear();
}catch(CharacterCodingException ex) {
System.err.println(ex);
}
}
}
}
如果上面的程序没有问题,运行后将会打印出 BrasilOpen.txt 的内容到控制台。