JAVA映射文件到内存_java nio 映射文件内容到内存

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 的内容到控制台。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值