We help IT Professionals succeed at work.

Standard way to perform file locks in Java?

jjacksn
jjacksn asked
on
Medium Priority
242 Views
Last Modified: 2010-03-31
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.  
Comment
Watch Question

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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?

Author

Commented:
also, what occurs if the file does not exist?

Author

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();

Author

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 = 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.


            
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

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

Author

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 sasReader.read(f) is throwing an error?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
because u have a lock on it

Author

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?  
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
No file locks don't work like that.

Author

Commented:
I see, ok.  is there any standard way to do that or should I wrap a while look into a method, effectively blocking...?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
yes I'd be encapsulating access to that file in a class, and using synchronisation to control access to it.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.