我正在使用 RandomAccessFile 打开大文件(~ 200 MB),然后获取它的 Channel。我正在尝试将一些数据映射到 MappedByteBuffer,但出现异常:
Channel not open for writing - cannot extend file for required size.
我不明白为什么映射方法尝试写入文件 - 映射区域小于文件大小!为什么在这种情况下它试图扩展文件?
更新
问题解决了,原来我认为第三个参数表示文件中的索引位置,它是缓冲区的长度。
最佳答案
来自文档:
Parameters:
position The position within the file at which the mapped region is to start; must be non-negative
size The size of the region to be mapped; must be non-negative and no greater than java.lang.Integer.MAX_VALUE
您的文件大小到底是多少? 您是否尝试映射超出文件大小的偏移+大小?
关于java - FileChannel 映射 - 无法扩展写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22081443/