title: File path not correct for Path object in java
body: <p>I have a program which monitors any changes in the directory.</p>
  
  <p>This is the output of the program.</p>
  
  <pre><code>C:\REST API\source\data_id.txt
  
  The new file :data_id.txtEvent :ENTRY_CREATE
  
  C:\REST API\source\New folder    
  
  The new file :New folderEvent :ENTRY_CREATE
  
  C:\REST API\source\NhuPhutBanDau-NooPhuocThinh-4549467.doc
  
  The new file :NhuPhutBanDau-NooPhuocThinh-4549467.docEvent :ENTRY_CREATE
  </code></pre>
  
  <p>My program monitors the root directory <code>C:/REST API/source</code> for any changes. When I copy <code>data_id.txt</code> file into the root directory, it's able to detect new file as shown in the output above and the directory as well. </p>
  
  <p>After this, when I try to create a folder, it's also able to detect that there's a new folder and shows the new folder directory in the output. </p>
  
  <p>But the problem is that when I try to put a file into the newly created folder, it's again able to detect changes in the new folder  <code>C:\REST API\source\New folder</code> which is not the root directory set in my program which is <code>C:\REST API\source</code>. </p>
  
  <p>I need the program to detect changes only in the root directory.</p>
  
  <p>Can someone guide me on this?</p>
  
  <p>This is my source code:</p>
  
  <pre><code>public class fileStatus {
  
      private final WatchService svc = null;
      private final HashMap&lt;WatchKey , Path&gt; keyMap = null;
  
      public static void main(String [] args) throws FileNotFoundException, IOException, JSONException, InterruptedException
      {
       try(WatchService svc = FileSystems.getDefault().newWatchService()) 
          {
              Map&lt;WatchKey, Path&gt; keyMap = new HashMap&lt;&gt;();
              Path path = Paths.get("C:/REST API/source/");
              fileStatus fs = new fileStatus();
              fs.registerAll(path,keyMap,svc);
              WatchKey wk ;
             do 
             {
  
                  wk = svc.take();
                  Path dir = keyMap.get(wk);
                  for(WatchEvent&lt;?&gt; event : wk.pollEvents())
                  {
                      WatchEvent.Kind&lt;?&gt; type = event.kind();
                      Path fileName = (Path)event.context();
                      Path child = path.resolve(fileName);
                      System.out.println(child);
                      if (Files.isDirectory(child, LinkOption.NOFOLLOW_LINKS))
                      {
                          if(type == StandardWatchEventKinds.ENTRY_CREATE)
                          {
                              registerAll(child,keyMap,svc);
                          }
                      }
                      System.out.println("\nThe new file :"+fileName+ "Event :" +type);  
                  }
             }while(wk.reset());
          }
       catch(IOException e)
       {
           e.printStackTrace();
       }
  
      }
  
      private static void registerAll(Path path, Map&lt;WatchKey, Path&gt; keyMap, WatchService svc) throws IOException 
      {
          Files.walkFileTree(path,new SimpleFileVisitor&lt;Path&gt;()
                  {
                     public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs) throws IOException
                     {
                         if(attrs.isDirectory())
                         {
                             keyMap.put(path.register(svc, StandardWatchEventKinds.ENTRY_CREATE),path);
                         }
                         return FileVisitResult.CONTINUE;
                     }
                  });
  
      }
  
  }
  </code></pre>