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.  
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Manikandan ThiagarajanSenior consultantCommented:
jjacksnAuthor Commented:
two questions:  

1.  If the server crashes after creating the lock, will the channel lock block upon restart?

2.  what does the channel do?
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

jjacksnAuthor Commented:
also, what occurs if the file does not exist?
jjacksnAuthor Commented:
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();
jjacksnAuthor Commented:
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 =;

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.
jjacksnAuthor Commented:
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 is throwing an error?
because u have a lock on it
jjacksnAuthor Commented:
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?  
No file locks don't work like that.
jjacksnAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.