Java FileLock release

Description

Java FileLock release

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

public class Main {
  public static void main(String[] args) throws Exception {
    RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");
    FileChannel fileChannel = raf.getChannel();
    FileLock lock = null;//from  w  w w  .  ja v  a2  s  .  c  o  m
    try {
      lock = fileChannel.lock(0, 10, true);

      /* Work with the file here */
      lock.release();
    } catch (IOException e) {
      // Handle the exception
    }
    raf.close();
  }
}



PreviousNext

Related