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