Java 中尝试从二进制文件读取 Int 时出现 BufferUnderflowException

BufferUnderflowException while trying to read an Int from a binary file in Java

我正在尝试读取代表 int 的 4 个字节,位于二进制文件中的字节位置 64。

这是我试过的:

package testbinaryfile2;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class TestBinaryFile2 {

    public static void main(String[] args) throws IOException {
        
            FileChannel fc;    
            ByteBuffer indexField = ByteBuffer.allocate(4);
            
            fc = (FileChannel.open(Paths.get("myBinaryFile.bin"), StandardOpenOption.READ));                        
            fc.position(64);
            fc.read(indexField);
            System.out.println(indexField.getInt());
            
    }
            
}

这是我得到的错误:

run:
Exception in thread "main" java.nio.BufferUnderflowException
    at java.nio.Buffer.nextGetIndex(Buffer.java:509)
    at java.nio.HeapByteBuffer.getInt(HeapByteBuffer.java:373)
    at testbinaryfile2.TestBinaryFile2.main(TestBinaryFile2.java:30)
/home/user/.cache/netbeans/11.3/executor-snippets/run.xml:111: The following error occurred while executing this line:
/home/user/.cache/netbeans/11.3/executor-snippets/run.xml:94: Java returned: 1
BUILD FAILED (total time: 0 seconds)

读取indexField后指向其末尾,所以在获取int之前需要使用.rewind()方法

此代码有效:

package testbinaryfile2;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class TestBinaryFile2 {

    public static void main(String[] args) throws IOException {
        
            FileChannel fc;    
            ByteBuffer indexField = ByteBuffer.allocate(4);
            
            fc = (FileChannel.open(Paths.get("myBinaryFile.bin"), StandardOpenOption.READ));                        
            fc.position(64);
            fc.read(indexField);
            indexField.rewind();    // <-- ADD THIS
            System.out.println(indexField.getInt());
            
    }
            
}

此外,我得到了使用映射缓冲区的建议:

  public static void main(String[] args) throws Exception {
    FileChannel fc = (FileChannel.open(Paths.get("myBinaryFile.bin"), StandardOpenOption.READ));
    MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    buffer.position(64);
    System.out.println(buffer.getInt());
  }

但我想知道 fc.map 是否一次读取整个文件并使用与文件大小一样多的内存。