?
Solved

Standard way to perform file locks in Java?

Posted on 2006-03-27
15
Medium Priority
?
226 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.  
0
Comment
Question by:jjacksn
  • 7
  • 5
13 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 16307346
0
 
LVL 9

Assisted Solution

by:Manikandan Thiagarajan
Manikandan Thiagarajan earned 1000 total points
ID: 16307698
0
 
LVL 5

Author Comment

by:jjacksn
ID: 16310134
two questions:  

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

2.  what does the channel do?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:jjacksn
ID: 16310143
also, what occurs if the file does not exist?
0
 
LVL 5

Author Comment

by:jjacksn
ID: 16310344
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();
0
 
LVL 5

Author Comment

by:jjacksn
ID: 16310468
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.


            
0
 
LVL 92

Expert Comment

by:objects
ID: 16318975

> 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.
0
 
LVL 5

Author Comment

by:jjacksn
ID: 16323868
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?
0
 
LVL 92

Expert Comment

by:objects
ID: 16327349
because u have a lock on it
0
 
LVL 5

Author Comment

by:jjacksn
ID: 16327363
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?  
0
 
LVL 92

Expert Comment

by:objects
ID: 16327392
No file locks don't work like that.
0
 
LVL 5

Author Comment

by:jjacksn
ID: 16327411
I see, ok.  is there any standard way to do that or should I wrap a while look into a method, effectively blocking...?
0
 
LVL 92

Expert Comment

by:objects
ID: 16327445
yes I'd be encapsulating access to that file in a class, and using synchronisation to control access to it.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question