Code example for RandomAccessFile

Methods: close, getChannel

0
import java.nio.channels.FileChannel; 
 
public class FileHole { 
    public static void main(String[] args) throws IOException {
        File tmpFile = new File("/home/rshf/tmp/channel.dat");
        RandomAccessFile file = new RandomAccessFile(tmpFile, "rw");
        FileChannel fChannel = file.getChannel();
 
        ByteBuffer bBuffer = ByteBuffer.allocateDirect(1024);
         
        putData(0, bBuffer, fChannel);
        putData(100, bBuffer, fChannel);
        putData(200, bBuffer, fChannel);
         
        fChannel.close();
        file.close();
    } 
 
    public static void putData(int position, ByteBuffer bBuffer, FileChannel fChannel) throws IOException {
        String conent = "*<--- location " + position;
        bBuffer.clear();