为什么RandomAccessFile使用int作为偏移量

Ill*_*huk 3 java int random-access long-integer

我正在编写一些数据访问测试实现,我需要随机访问文件内容.这是代码:

RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rwd");
final byte b[] = IOUtils.toByteArray(source);
randomAccessFile.write(b, (int) offset, size);
Run Code Online (Sandbox Code Playgroud)

其中offset的类型为long.为什么RandomAccessFile没有提供方法:

public void write(byte b[], long off, int len)
Run Code Online (Sandbox Code Playgroud)

如何覆盖此问题?

Tom*_*ine 15

我想你正在寻找这种seek方法.

偏移量write是数组的偏移量.数组有int偏移量.已经提出了"长阵列"的建议,但如果实施了这些建议,你仍然需要过载.

NIO中的映射文件存在一个问题,即由于MappedByteBuffer.position某些原因仅使用等效的int.见CR 6347833(9票).