Locking Files in Java
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 —
tryLock() — for this purpose.
via DZone.com Feed https://dzone.com
May 19, 2017 at 11:09AM