The files are written in place by processes that I have no control over.

The problem I have with that code is that my callback is triggered when the File is initially created. I need it to trigger when the file has been changed and the write to the file has completed. (maybe by detecting when the file stopped changing)

IOW you want to know when the writing process closes the file?
–
finnwJan 19 '11 at 23:36

I think I basically need a debounce implementation for Java
–
levinalexJan 19 '11 at 23:36

re closing the file: Yes, if that works that would solve it. (Is there a way to get that information whithout any control over the writer?) Could I just try to get an exclusive lock on the file? Does that work across platforms?
–
levinalexJan 19 '11 at 23:39

Check out Java 7's WatchService API ,may be this will have some functionality you desire.
–
EmilJan 20 '11 at 5:05

4 Answers
4

I had a similar problem. At first I thought I could use the FileWatcher service, but it doesn't work on remote volumes, and I had to monitor incoming files via a network mounted drive.

Then I thought I could simply monitor the change in file size over a period of time and consider the file done once the file size had stabilized (as fmucar suggested). But I found that in some instances on large files, the hosting system would report the full size of the file it was copying, rather than the number of bytes it had written to disk. This of course made the file appear stable, and my detector would catch the file while it was still in the process of being written.

I eventually was able to get the monitor to work, by employing a FileInputStream exception, which worked wonderfully in detecting whether a file was being written to, even when the file was on a network mounted drive.

I don't think you can achieve what you want unless you have some file system constraints and guarantees. For example, what if you have the following scenario :

File X created

A bunch of change events are triggered that correspond with writing out of file X

A lot of time passes with no updates to file X

File X is updated.

If file X cannot be updated after it's written out, you can have a thread of execution that calculates the elapsed time from the last update to now, and after some interval decides that the file write is complete. But even this has issues. If the file system is hung, and the write does not occur for some time, you could erroneously conclude that the file is finished writing out.

You're right. But fortunately this doesn't happen in my case. (Steps 1 to 3 do happen, Step 4 doesn't) The file is written to reasonably quickly
–
levinalexJan 19 '11 at 23:45

In that case, you can do what I said. There are still drawbacks to this approach. For one, you'd need a thread per file. Another, is choosing a heuristic value for elapsed time.
–
Amir AfghaniJan 20 '11 at 0:04

Yeah. I made a followup question Debounce in Java for the thread solution. (reasonably quickly is 100ms in my case) I'm really interested if the locking approach would work because that would be a lot more elegant.
–
levinalexJan 20 '11 at 0:17