title: Avoid detecting incomplete files when watching a directory for changes in java
body: <p>I am watching a directory for incoming files (using <a href="http://commons.apache.org/io/api-release/org/apache/commons/io/monitor/FileAlterationObserver.html">FileAlterationObserver</a> from apache commons).</p>
  
  <pre><code>class Example implements FileAlterationListener {
      public void prepare() {
          File directory = new File("/tmp/incoming");
          FileAlterationObserver observer = new FileAlterationObserver(directory);
          observer.addListener(this);
          FileAlterationMonitor monitor = new FileAlterationMonitor(10);
          monitor.addObserver(observer);
          monitor.start();
          // ...
      }
  
      public void handleFile(File f) {
          // FIXME: this should be called when the writes that 
          // created the file have completed, not before
      }
  
      public void onFileCreate(File f) {
          handleFile(f);
      }
  
      public void onFileChange(File f) {
          handleFile(f);
      }
  }
  </code></pre>
  
  <p>The files are written in place by processes that I have no control over.</p>
  
  <p>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 <em>stopped</em> changing)</p>
  
  <p>What's the best way to do that?</p>