首页javarandom_accessJava I/O - 如何使用FileChannel将int写入RandomAccessFile

Java I/O - 如何使用FileChannel将int写入RandomAccessFile

我们想知道如何使用FileChannel将int写入RandomAccessFile。
 


import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;

public class MainClass {
  public static void main(String[] args) throws IOException {
    FileChannel fc = new RandomAccessFile("temp.tmp", "rw").getChannel();
    IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
    for (int i = 0; i < 10; i++)
      ib.put(i);
    fc.close();

  }
}