1

I have code as follows:

for (;;) {
     // retrieve key
     WatchKey key = watcher.take();
 // process events
 for (WatchEvent<?> event: key.pollEvents()) {
     :
 }

 // reset the key
 boolean valid = key.reset();
}
  • Now that Jave 7 has been out for a little while, is there a way to determine if a large file has completed being created using the nio library, or am I coding around that?
Steve
  • 4,457
  • 12
  • 48
  • 89
  • You could spawn a `Thread` to do the file creation and use `.join()` on it right before resetting the key. That way you will make sure the reset is not triggered before file operation is completed. – Mohammad Najar Oct 09 '14 at 16:43
  • I'm not sure we're on the same page here. I'm not doing any file creation, just watching for a file. My question is more, between the take() and the reset(), is anything polling for files? – Steve Oct 09 '14 at 16:57
  • I was trying to answer your second question. You can use the approach I mention to determine if any operation has been completed (e.g. file creation, etc.). – Mohammad Najar Oct 09 '14 at 17:05
  • Right, but my code doesn't contain the process that creates the file. So I can't put that code into a seperate thread. (If that's what I was doing that would of course work, but then I wouldn't be doing a file watcher in the first place). I also removed my first question. Testing has revealed it not to be an issue. – Steve Oct 09 '14 at 19:05

1 Answers1

0

https://stackoverflow.com/a/10535018/835523

The answer is here. Basically (on a windows system at least) you can simply attempt to open a FileInputStream, and loop around until it succeeds (it'll fail if the file is still being written to).

Community
  • 1
  • 1
Steve
  • 4,457
  • 12
  • 48
  • 89