相信能遇到这个问题的人们都是成年人了 所以相信我不用写的那么详细大家应该能看懂
今天实验java的内存映射文件时候发生错误 文件是在eclipse项目下直接创建的 extra/txt/testStream.txt
java语句
Path filename = Paths.get("extra/txt/testStream.txt");
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE,0,channel.size());
报错如下
Exception in thread "main" java.nio.channels.NonWritableChannelException
at sun.nio.ch.FileChannelImpl.map(Unknown Source)
at test.fileop.MemoryMapF.main(MemoryMapF.java:27)
错误发生在第二句 看提示说写出现错误 改成
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY,0,channel.size());
报错消失
查询api文档后
The options parameter determines how the file is opened. The READ and WRITE options determine if the file should be opened for reading and/or writing. If neither option (or the APPEND option) is contained in the array then the file is opened for reading.
才发现这个channel默认创建方式只能读 所以修改即可