Java: How to determine if a file is in use?

K

Krick

In Java, I need to be able to check whether a file is in use.

So far, the only the only method I can come up with is this (in pseudo code)...


1. make a copy of the original file (block copy the binary contents)
2. attempt to delete the original file
3a. if delete fails, file is in use, delete copy
3b. if delete succeeds, rename copy to original name


Will this work?
Am I missing anything?
What about cross platform compatability?


....
Krick
 
S

Shripathi Kamath

Krick said:
In Java, I need to be able to check whether a file is in use.

So far, the only the only method I can come up with is this (in pseudo code)...


1. make a copy of the original file (block copy the binary contents)
2. attempt to delete the original file
3a. if delete fails, file is in use, delete copy
3b. if delete succeeds, rename copy to original name


Will this work?

If you are lucky, and are willing to live with all the side effects.

- 2. for example, may fail when the file is not in use, and is marked
read-only, or is protected.
- Your copying may results in throwing away changes to the file after a
block that has been copied is subsequently changed, *before* 3b.

Am I missing anything?
Maybe. The approach does not sound too robust especially because detecting
that a file is or is not in use, does not guarantee that for a subsequent
operation *immediately* thereafter, the condition is guaranteed.

Suppose your logic was

a. Determine if file is in use
b. If file is in use then do Step A
c. If not do Step B

If the intention is that Step A is done only if the file is in use, then you
cannot be assured of that. Right after a., the file could end up being NOT
in use.
What about cross platform compatability?

You'll need to test it on platforms that you want your app to compatible
across.
Check the file locking support in J2SE 1.4 (It is not universally supported
on all platforms, though)

HTH,
 
R

Richard Kuhler

Krick said:
In Java, I need to be able to check whether a file is in use.
<snip>

If it's your process that would be using the file then you could look at
the source for java.util.logging.FileHandler for an example of how they
ensure two loggers don't open the same file.

Richard Kuhler
 
D

David Zimmerman

Krick said:
In Java, I need to be able to check whether a file is in use.

So far, the only the only method I can come up with is this (in pseudo code)...


1. make a copy of the original file (block copy the binary contents)
2. attempt to delete the original file
3a. if delete fails, file is in use, delete copy
3b. if delete succeeds, rename copy to original name


Will this work?
Am I missing anything?
What about cross platform compatability?
UNIX will be perfectly happy to let you delete a file that's in use.
When the other use of that file completes, the file will disappear.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top