title: Use jpathwatch to find out when a program finished writing to a file
body: <p>I'm currently using jpathwatch to watch for new files created in a folder. All fine, but I need to find out when a program finished writing to a file.</p>
  
  <p>The library's author describes on his website (<a href="http://jpathwatch.wordpress.com/faq/" rel="nofollow noreferrer">http://jpathwatch.wordpress.com/faq/</a>) how that's done but somehow I don't have a clue how to do that. Maybe it's described a bit unclear or I just don't get it.</p>
  
  <p>I would like to ask whether you could give me a snippet which demonstrates how to do that.</p>
  
  <p>This is the basic construct:</p>
  
  <pre><code>public void run() {
  
      while (true) {
          WatchKey signalledKey;
          try {
              signalledKey = watchService.take();
          } catch (InterruptedException ix) {
              continue;
          } catch (ClosedWatchServiceException cwse) {
              break;
          }
  
          List&lt;WatchEvent&lt;?&gt;&gt; list = signalledKey.pollEvents();
          signalledKey.reset();
  
          for (WatchEvent&lt;?&gt; e : list) {
              if (e.kind() == StandardWatchEventKind.ENTRY_CREATE) {
                  Path context = (Path) e.context();
                  String filename = context.toString();
  
                  // do something
              } else if (e.kind() == StandardWatchEventKind.ENTRY_DELETE) {
                  Path context = (Path) e.context();
                  String filename = context.toString();
  
                  // do something
              } else if (e.kind() == StandardWatchEventKind.OVERFLOW) {
              }
          }
      }
  }
  </code></pre>