Creating a File Lock on a File

The behavior of the file lock is platform-dependent. On some platforms, the file lock is advisory, which means that unless an application checks for a file lock, it will not be prevented from accessing the file. On other platforms, the file lock is mandatory, which means that a file lock prevents any application from accessing the file.
try {
    // Get a file channel for the file
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

    // Use the file channel to create a lock on the file.
    // This method blocks until it can retrieve the lock.
    FileLock lock = channel.lock();

    // Try acquiring the lock without blocking. This method returns
    // null or throws an exception if the file is already locked.
    try {
        lock = channel.tryLock();
    } catch (OverlappingFileLockException e) {
        // File is already locked in this thread or virtual machine
    }

    // Release the lock
    lock.release();

    // Close the file
    channel.close();
} catch (Exception e) {
}

Comments

18 Mar 2010 - 11:16pm by Patrik Eschle (not verified)

channel.tryLock() does not throw an exception, but returns null if the lock is already busy.

8 May 2010 - 4:07am by Leonard Lametree (not verified)

Thank you for the correction, Patrik.

Jeez. You have to love all these people who post examples when they don't know what they're doing, googling for answers is a minefield of bad advice. Bad depot farmer! No ad click cookie for you!

22 Jul 2010 - 5:11am by Anonymous (not verified)

Actually tryLock() can throw an exception if the file is already locked in the same JVM (very likely to happen in an application server).
So, Patrick and Leonard are wrong and, besides, Leonard was unnecessarily rude ( I love all these people who comment...).

22 Sep 2010 - 5:05am by Anonymous (not verified)

I want to ask a question. When the lock is held, if jvm is terminated abnormally (for example kill -9), how is the lock released? Thanks

9 Nov 2010 - 6:12am by Anonymous (not verified)

very naic

9 Nov 2010 - 6:13am by Anonymous (not verified)

very nice

9 Nov 2010 - 6:16am by Anonymous (not verified)

i would like to use this software.

27 Jan 2012 - 10:05am by Conyers (not verified)

This site is like a caslsroom, except I don't hate it. lol

2 Feb 2012 - 9:34pm by Pradeep (not verified)

Instead of using channel.close(), try lock.channel.close(). It would be a good option while accessing inside static methods.

2 Feb 2012 - 9:41pm by Pradeep (not verified)

Instead of using channel.close(), try lock.channel.close(). It would be a good option while accessing inside static methods.

3 Mar 2012 - 6:51pm by orlando (not verified)

any body can help me to make a code example of file lock for java.?and show the output.
thank you.

10 Apr 2012 - 4:07am by jay-pee c moro (not verified)

how to save of code to file

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.