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

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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!

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

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 }}