tags:

views:

365

answers:

4

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.

A: 

Do you mean that you're waiting for the lastModified time to settle? At best that will be a bit hit-and-miss.

How about trying to open the file with write access (appending rather than truncating the file, of course)? That won't succeed if another process is still trying to write to it. It's a bit ugly, particularly as it's likely to be a case of using exceptions for flow control (ick) but I think it'll work.

Jon Skeet
Doesn't that assume that you have write access to the directory being monitored? (I don't have a better idea, mind...)
Simon Nickerson
Thanks.I tried the approach and it works on my development machine but same thought as the earlier comment as I am not sure about the deployment user permissions
Krishna Kumar
@simonn: Yes, that does rely on you having write permission, not to the directory but for the file.
Jon Skeet
Will this be OS-dependent ? I can see it working for Windows, but I'm not so sure for *nix.
Brian Agnew
I don't *think* Unix will allow that (at least without explicitly asking for it).
Jon Skeet
A: 

If I understood the question correctly, you're looking for a way to distinguish whether the copying of a file is complete or still in progress?

How about comparing the size of the source and destination file (i.e. file.length())? If they're equal, then copying is complete. Otherwise, it's still in progress.

I'm not sure it's efficient since it would still require polling. But it "might" work.

teriz
+2  A: 

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.

paxdiablo
I tried the second approach on windows and found that file.length() gives me the final size (after copy completion) even when the copy is in progress. Any thoughts on this
Krishna Kumar
Yes, either use the same approach but with last-updated-time or use both size and time. But I'm surprised at your assertion - what is actually doing the copying? Explorer won't create the whole file up front. Bittorrent and other download accelerators are the only thing I've seen do that.
paxdiablo
I am doing manual copy/paste of file in explorer
Krishna Kumar
Interesting, when I do cut'n'paste of a big file in Explorer, it continuously increases the file size as it's copying. How big is your file? How long does it take to copy?
paxdiablo
@Pax yes, my version of WS03 does that too: the copied file instantaneously acquire the final size, even though it just started copying. And that's with really big files (more than 10Gb)
Varkhan
Maybe MSFT made this change in later Windows version. Best bet then is to use the last modification time (assuming that changes as the file gets copied).
paxdiablo
+1  A: 

You could look into online file upload with progressbar techniques - they use OutputStreamListener and custom writer to notify the listener about bytes written.

http://www.missiondata.com/blog/java/28/file-upload-progress-with-ajax-and-java-and-prototype/

http://stackoverflow.com/questions/254719/file-upload-with-java-with-progress-bar

miceuz