Java RandomAccessFile create read only FileChannel

Description

Java RandomAccessFile create read only FileChannel


import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
  public static void main(String[] argv) throws Exception {
    File file = new File("Main.java");

    FileChannel roChannel = new RandomAccessFile(file, "r").getChannel();
    ByteBuffer roBuf = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) roChannel.size());

    System.out.println(roBuf.capacity());

    roChannel.close();//from  w  w w  .  ja v a2 s . co  m

  }
}



PreviousNext

Related