首页javanio_bufferJava I/O - 如何将FileChannel映射到IntBuffer并从IntBuffer读取

Java I/O - 如何将FileChannel映射到IntBuffer并从IntBuffer读取

我们想知道如何将FileChannel映射到IntBuffer并从IntBuffer读取。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;

public class MainClass {
  public static void main(String[] args) throws IOException {
    FileChannel fc = new FileInputStream(new File("temp.tmp")).getChannel();
    IntBuffer ib = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asIntBuffer();
    while (ib.hasRemaining())
      ib.get();
    fc.close();
  }
}