File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Wait for process to finish before opening Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Wait for process to finish before opening" Watch "Wait for process to finish before opening" New topic

Wait for process to finish before opening

Mars Mondal

Joined: Jan 01, 2011
Posts: 14
I have a implemented a listener that notifies if we receive a new file in a particular directory. This is implemented by polling and using a TimerTask.
Now the program is so set up that once it receives a new file it calls another java program that opens the file and validates whether it is the correct file. My problem is that since the polling happens a specified number of seconds later there can arise a case in which a file is being copied in that directory and hence is locked by windows.

This throws an IOException since the other java program that tries to open it for validation cannot ("File is being used by another process").

Is there a way I can know when windows has finished copying and then call the second program to do the validations from java?

I will be more than happy to post code snippets if someone needs them in order to help.

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

No. The best way to arrange things is to have the process which is creating the file also tell you when it has finished. Where I work we have used various strategies. One strategy is to have the process create the main file, then create a second file with zero bytes, and for the receiving code to wait for that second file. Another strategy is to have the process create the main file with a name that the receiving code is not looking for, and then rename it after it's finished.

But no, there's no good way to tell if a file is still in use. Even in Windows, which tends to put locks on things which other OS's don't.
I agree. Here's the link:
subject: Wait for process to finish before opening
jQuery in Action, 3rd edition