jjacksn
asked on
Standard way to perform file locks in Java?
Is there a standard way to provide file locks in Java that will work across platforms? I just need to lock a file when I am writing to it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
also, what occurs if the file does not exist?
ASKER
This always throws and exception... any idea what is going on?
f = new File(fileString);
// Try acquiring the lock without blocking. This method returns
// null or throws an exception if the file is already locked.
Document doc;
FileChannel channel = null;
FileLock lock = null;
if (f.exists()) {
try {
// Get a file channel for the file
channel = new RandomAccessFile(f, "rw").getChannel();
// Use the file channel to create a lock on the file.
// This method blocks until it can retrieve the lock.
lock = channel.lock();
lock = channel.tryLock();
f = new File(fileString);
// Try acquiring the lock without blocking. This method returns
// null or throws an exception if the file is already locked.
Document doc;
FileChannel channel = null;
FileLock lock = null;
if (f.exists()) {
try {
// Get a file channel for the file
channel = new RandomAccessFile(f, "rw").getChannel();
// Use the file channel to create a lock on the file.
// This method blocks until it can retrieve the lock.
lock = channel.lock();
lock = channel.tryLock();
ASKER
ok, that was dumb, i'm not supposed to call lock and then trylock, just one or the other.
however, I want to call
doc = saxReader.read(f);
with the lock on (the reason I am locking is to make sure no one else is using the file while I am reading and writing to it), the saxReader is throwing an exception.
however, I want to call
doc = saxReader.read(f);
with the lock on (the reason I am locking is to make sure no one else is using the file while I am reading and writing to it), the saxReader is throwing an exception.
> the reason I am locking is to make sure no one else is using the file while I am reading and writing to it
the OS should look after that for you.
ASKER
If I just use the OS for that, can I make a blocking call without using the Lock mechanism without hand coding a while loop?
also, do you know why the sasReader.read(f) is throwing an error?
also, do you know why the sasReader.read(f) is throwing an error?
because u have a lock on it
ASKER
ok, to clarify what I am trying to do:
I have an append only file, and when someone is appending it, no one should be reading or writing to it. (pretty standard).
So, i need some mechanism for this to occur. However, rather than a call failing on File Open, I'm looking to block until the file is released. Is this not what locks are supposed to be used for?
I have an append only file, and when someone is appending it, no one should be reading or writing to it. (pretty standard).
So, i need some mechanism for this to occur. However, rather than a call failing on File Open, I'm looking to block until the file is released. Is this not what locks are supposed to be used for?
No file locks don't work like that.
ASKER
I see, ok. is there any standard way to do that or should I wrap a while look into a method, effectively blocking...?
yes I'd be encapsulating access to that file in a class, and using synchronisation to control access to it.
ASKER
1. If the server crashes after creating the lock, will the channel lock block upon restart?
2. what does the channel do?