How to Wait for windows process to finish before opening file in java

Eosphorus :

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.

Thanks

GETah :

This one is a bit tricky. It would have been a piece of cake if you could control or at least communicate with the program copying the file but this won't be possible with Windows I guess. I had to deal with a similar problem a while ago with SFU software, I resolved it by looping on trying to open the file for writing until it becomes available.

To avoid high CPU usage while looping, checking the file can be done at an exponential distribution rate.

EDIT A possible solution:

File fileToCopy = File(String pathname);
int sleepTime = 1000; // Sleep 1 second
while(!fileToCopy .canWrite()){
    // Cannot write to file, windows still working on it
    Sleep(sleepTime);
    sleepTime *= 2; // Multiply sleep time by 2 (not really exponential but will do the trick)
    if(sleepTime > 30000){ 
        // Set a maximum sleep time to ensure we are not sleeping forever :)
        sleepTime = 30000;
    }
}
// Here, we have access to the file, go process it
processFile(fileToCopy);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Wait for process to finish before proceeding in Java

Node.JS: how to wait for a process to finish before continuing?

Wait for async process to finish before redirect in koajs

How to wait for a thread to finish before another thread starts in Java/Android?

Run process before opening file

Wait for a child process to finish before starting new child process

Wait for busboy on#file to finish before calling on#finish

Android how to wait for code to finish before continuing

ReactJs: How to wait for componentDidMount() to finish before rendering?

How to wait for Function to finish before continuing

How to wait for a RecyclerView animation to finish before resizing it?

How to wait for a method to finish completely before proceeding?

How to wait for getDownloadURL to finish before proceeding?

How to wait SparkContext to finish all process?

How to wait for the copying process (cp) to finish?

Wait for file to finish writing to disk before processing in Node.js

Wait for async file write to finish before exiting main thread

Windows - Opening a file from code and wait for it to be closed

How to wait for a process in Java

Java cancel Future - How to wait for finish?

How to wait for a Java applet to finish loading on Safari?

Java threading - How to properly wait for threads to finish?

How to wait for request to finish before execute the next operation (Xcode 11)

NodeJS Plotly - how to wait for getImage to finish before continuing

C# how to wait for a webpage to finish loading before continuing in watin

Perl: how to wait for a curl step to finish before processing to the next

How to wait for knockout page to finish loading before printing in DotNetBrowser?

How to wait for a observable method to finish before proceeding in Angular 2

How to wait for ajax success handler to finish before executing another