Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know whether a file copying is 'in progress'/complete in java (1.6) [duplicate]

Tags:

java

file-io

I am writing a directory monitoring utility in java(1.6) using polling at certain intervals using lastModified long value as the indication of change. I found that when my polling interval is small (seconds) and the copied file is big then the change event is fired before the actual completion of file copying.

I would like to know whether there is a way I can find the status of file like in transit, complete etc.

Environments: Java 1.6; expected to work on windows and linux.

like image 248
Krishna Kumar Avatar asked Apr 15 '09 05:04

Krishna Kumar


2 Answers

There are two approaches I've used in the past which are platform agnostic.

1/ This was for FTP transfers where I controlled what was put, so it may not be directly relevant.

Basically, whatever is putting a file file.txt will, when it's finished, also put a small (probably zero-byte) dummy file called file.txt.marker (for example).

That way, the monitoring tool just looks for the marker file to appear and, when it does, it knows the real file is complete. It can then process the real file and delete the marker.

2/ An unchanged duration.

Have your monitor program wait until the file is unchanged for N seconds (where N is reasonably guaranteed to be large enough that the file will be finished).

For example, if the file size hasn't changed in 60 seconds, there's a good chance it's finished.

There's a balancing act between not thinking the file is finished just because there's no activity on it, and the wait once it is finished before you can start processing it. This is less of a problem for local copying than FTP.

like image 116
paxdiablo Avatar answered Sep 22 '22 06:09

paxdiablo


This solution worked for me:

File ff = new File(fileStr);

if(ff.exists()) {

    for(int timeout = 100; timeout>0; timeout--) {
        RandomAccessFile ran = null;

        try {
            ran = new RandomAccessFile(ff, "rw");
            break; // no errors, done waiting
        } catch (Exception ex) {
            System.out.println("timeout: " + timeout + ": " + ex.getMessage());
        } finally {
            if(ran != null) try {
                ran.close();
            } catch (IOException ex) {
                //do nothing
            }

            ran = null;
        }

        try {
            Thread.sleep(100); // wait a bit then try again
        } catch (InterruptedException ex) {
            //do nothing
        }
    }

    System.out.println("File lockable: " + fileStr +
                 (ff.exists()?" exists":" deleted during process"));
} else {
    System.out.println("File does not exist: " + fileStr);
}

This solution relies on the fact that you can't open the file for writing if another process has it open. It will stay in the loop until the timeout value is reached or the file can be opened. The timeout values will need to be adjusted depending on the application's actual needs. I also tried this method with channels and tryLock(), but it didn't seem to be necessary.

like image 25
Mr Ed Avatar answered Sep 19 '22 06:09

Mr Ed