FileChannel: tryLock() : FileChannel « java.nio.channels « Java by API






FileChannel: tryLock()

 
import java.io.FileOutputStream;
import java.nio.channels.FileLock;

public class Main {
  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("file.txt");
    FileLock fl = fos.getChannel().tryLock();
    if (fl != null) {
      System.out.println("Locked File");
      Thread.sleep(100);
      fl.release();
      System.out.println("Released Lock");
    }
    fos.close();
  }
}

   
  








Related examples in the same category

1.FileChannel.MapMode.READ_ONLY
2.FileChannel.MapMode.READ_WRITE
3.FileChannel: close()
4.FileChannel: lock(long position, long size, boolean shared)
5.FileChannel: map(MapMode mode, long position, long size)
6.FileChannel: position()
7.FileChannel: position(long newPosition)
8.FileChannel: read(ByteBuffer dst)
9.FileChannel: size()
10.FileChannel: transferFrom(ReadableByteChannel src, long position, long count)
11.FileChannel: transferTo(long position, long count, WritableByteChannel target)
12.FileChannel: write(MappedByteBuffer buffer)
13.FileChannel: write(ByteBuffer buffer)