试验内存映射文件时候的错误 Exception in thread "main" java.nio.channels.NonWritableChannelException at sun.nio.c

相信能遇到这个问题的人们都是成年人了 所以相信我不用写的那么详细大家应该能看懂

今天实验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默认创建方式只能读 所以修改即可
 


 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值