Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Locking Files in Java

DZone's Guide to

Locking Files in Java

Time to get to know your locks! Whether you need blocking or non-blocking locks or want to know more about how they work, this quick example will set you straight.

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

In most I/O-based applications, we deal with concurrency, where several processes do reads/writes to a file. While reading/writing to a file, we need to ensure that it is not being used by other processes to ensure data integrity. The Java NIO API provides file locking at the operating system level (hence it is visible to other processes) so that other processes cannot access the file. The NIO API  allows the obtaining of shared or exclusive locks on either whole file or part of the file.

In Java, a file lock can be obtained using FileChannel, which provides two methods — lock() and tryLock() — for this purpose.

The lock()  method acquires an exclusive lock on entire file, whereas the lock(long position, long size, boolean shared) method can be used to acquire a lock on the given region of a ile. These two methods will block until the lock is obtained. Meanwhile, the methods tryLock() and tryLock(long position, long size, boolean shared) do the same, but they don't block and they return immediately.

These methods return a FileLock object, which can be used further to control the lock and, at the end, to release a lock. In the case of the  tryLock() method, if it fails to acquire a lock due to overlapping (lock already held by other process), then it returns null; if it fails due to some other reason, then an appropriate exception is thrown.

Now let us look at an example:

package com.test.nio.filelock;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FileLockTest {

    private static final Log LOG = LogFactory.getLog(FileLockTest.class);
    private static FileChannel fc;
    private static RandomAccessFile randomAccessFile;

    public FileLockTest(String fileName) throws FileNotFoundException {
        randomAccessFile = new RandomAccessFile(fileName, "rw");
    }

    public void writeToFileWithLock(String data) {

        fc = randomAccessFile.getChannel();
        ByteBuffer buffer = null;

        try (fc; randomAccessFile; FileLock fileLock = fc.tryLock()) {
            if (null != fileLock) {

                buffer = ByteBuffer.wrap(data.getBytes());
                buffer.put(data.toString().getBytes());
                buffer.flip();
                while (buffer.hasRemaining())
                    fc.write(buffer);
            }
        } catch (OverlappingFileLockException | IOException ex) {
            LOG.error("Exception occured while trying to get a lock on File... " + ex.getMessage());
        }
    }
}


Don't forget to lock the file when your application has several processes that can read/write. Happy learning!

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:
java ,nio ,file locking ,tutorial ,concurrency

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}