Java Reference
In-Depth Information
The following snippet of code maps the whole file
luci5.txt
in a read-only mode. It reads the file and displays
the contents on the standard output.
FileInputStream fis = new FileInputStream("luci5.txt");
FileChannel fc = fis.getChannel();
long startRegion = 0;
long endRegion = fc.size();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY,
startRegion, endRegion);
while(mbb.hasRemaining()) {
System.out.print((char)mbb.get());
}
fc.close();
File Locking
NIO supports file locking to synchronize access to a file. You have the ability to lock a region of a file or the entire file.
The file locking mechanism is handled by the operating system and therefore its exact effect is platform-dependent.
On some operating systems, a file lock is advisory, whereas on some, it is mandatory. Since it is handled by the
operating system, its effect is visible to other programs as well as to Java programs running in other JVMs.
■
an advisory lock lets other users use the file on which you have acquired the lock, but prevents them from
acquiring a lock on the same file. a mandatory lock forces the user to acquire a lock on the file before the file can be used.
Tip
There are two kinds of file locking:
exclusive
and
shared
. Only one program can hold an exclusive lock on a region
of a file. Multiple programs can hold shared locks on the same region of a file. You cannot mix an exclusive lock and a
shared lock on the same region of a file. If a program has a shared lock on a region, another program must wait to get
an exclusive lock on that region and vice versa. Some operating systems do not support a shared file lock, and in that
case, the request for a shared file lock is converted to a request for an exclusive file lock.
An object of the
FileLock
class, which is in the
java.nio.channels
package, represents a file lock. You acquire
a lock on a file by using the
lock()
or
tryLock()
method of the
FileChannel
object. The
lock()
method blocks if the
lock on the requested region of the file is not available. The
tryLock()
method does not block; it returns immediately.
It returns an object of the
FileLock
class if the lock was acquired; otherwise, it returns
null
.
Both
lock()
and
tryLock()
methods have two versions: one without an argument and another with three
arguments. The version without an argument locks the entire file. The version with three arguments accepts the
starting position of the region to lock, the number of bytes to lock, and a
boolean
flag to indicate if the lock is shared.
The
isShared()
method of the
FileLock
object returns
true
if the lock is shared; otherwise, it returns
false
.
The following snippet of code shows different ways of obtaining locks on a file. The exception handling code is
omitted for readability.
// Create a random access file and obtain a channel for it
RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");
FileChannel fileChannel = raf.getChannel();
// Get an exclusive lock on the file
FileLock lock = fileChannel.lock();