title: Directory watching for changes in java
body: <p>I am using WatchService to watch change in directory, in particular creation of new file in directory. Below is my code - </p>
  
  <pre><code>package watcher;
  
  import java.nio.file.*;
  import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
  import static java.nio.file.StandardWatchEventKinds.OVERFLOW;
  import java.io.*;
  
  
  public class Watch {
      public static void main(String[] args) throws IOException {
          Path dir = Paths.get("c:\\mk\\");
          WatchService service = FileSystems.getDefault().newWatchService();
          WatchKey key = dir.register(service, ENTRY_CREATE);
  
          System.out.println("Watching directory: "+dir.toString());
          for(;;){
              WatchKey key1;
              try {
                  key1 = service.take();
              } catch (InterruptedException x) {
                  break;
              }
  
              for (WatchEvent&lt;?&gt; event: key1.pollEvents()) {
                  WatchEvent.Kind&lt;?&gt; kind = event.kind();
  
                  if (kind == OVERFLOW) {
                      continue;
                  }
  
                  WatchEvent&lt;Path&gt; ev = (WatchEvent&lt;Path&gt;)event;
                  Path filename = ev.context();
                  Path child = dir.resolve(filename);
                  System.out.println("New file: "+child.toString()+" created.");
                  try{
                      FileInputStream in = new FileInputStream(child.toFile());
                      System.out.println("File opened for reading");
                      in.close();
                      System.out.println("File Closed");
                  }catch(Exception x){
                      x.printStackTrace();
                  }
              }
  
              boolean valid = key.reset();
              if (!valid) {
                  break;
              }
          }
      }
  }
  </code></pre>
  
  <p>When I create file inside "mk" directory, I am getting notification for that. But when I copy some file in this directory, I am getting exception on opening that copied file.</p>
  
  <p>My guess is Windows Copier dialog has still locked that file and I was unable to open that file. So basically I want to know is how to get notified for file has been closed by other process.</p>
  
  <p>Output of above code is like - </p>
  
  <pre><code>Watching directory: c:\mk
  New file: c:\mk\New Text Document (2).txt created.
  File opened for reading
  File Closed
  New file: c:\mk\Config.class created.
  java.io.FileNotFoundException: c:\mk\Config.class (The process cannot access the file because it is being used by another process)
          at java.io.FileInputStream.open(Native Method)
          at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:138)
          at watcher.Watch.main(Watch.java:36)
  New file: c:\mk\New Text Document (3).txt created.
  File opened for reading
  File Closed
  </code></pre>
  
  <p>Files "<code>New Text Document (2).txt</code>" and "<code>New Text Document (3).txt</code>" I have created but file "<code>Config.class</code>" I have copied from other directory.</p>
  
  <p>Please help me on this.</p>