0

My program is monitoring any file change in specified file path, if there's any new file coming, it will raise a notification, But going to fail when there's any sub folder created in parent folder. The file path for parent folder is being monitor C:/play but when there is a new sub folder created like C:/play/abcinside the parent folder, my program able to detect, but when i am trying to insert any file into abc folder, my program is not able to detect that new file has been created. I have tested various methods but unfortunately i can't let it work.Anyone able to provide me any guide on my reference link?My sample code is following the guide in my reference link

This is my source code after adding into the function for checking sub directory

public class fileStatus 
{
    public static void main(String [] args) throws InterruptedException
    {
    try(WatchService svc = FileSystems.getDefault().newWatchService()) 
    {
        Map<WatchKey, Path> keyMap = new HashMap<>();
        Path path = Paths.get("C:/play");
        fileStatus fd = new fileStatus();
        fd.registerAll(path);
        keyMap.put(path.register(svc,
            StandardWatchEventKinds.ENTRY_CREATE),
            path);
        WatchKey wk ;
       do 
       {
            wk = svc.take();
            Path dir = keyMap.get(wk);
            for(WatchEvent<?> event : wk.pollEvents())
            {
                WatchEvent.Kind<?> type = event.kind();
                Path fileName = (Path)event.context();
                System.out.println("\nThe new file :"+fileName+ "Event :" +type);  //print the new file name 
            }
       }while(wk.reset());
 }
catch(IOException e)
{
        System.out.println("Problem io  in somewhere");
}

}

     private void registerAll(Path path) throws IOException
    {
        Files.walkFileTree(path, new SimpleFileVisitor<Path>()
                {
                  @SuppressWarnings("unused")
                public FileVisitResult preVisitDireotry(Path path,BasicFileAttributes attrs) throws IOException
                  {
                      return FileVisitResult.CONTINUE;
                  }
                });

    } 
}



This is my reference code and my folder structure look like this,

/root
  /Folder A 
    /test.txt 
  /Folder B 
    /abc.txt
Community
  • 1
  • 1
yumi
  • 153
  • 8

1 Answers1

1

In short, you have only registered the parent directory to be watched. Any sub-directories you create will not be watched. See here.

Community
  • 1
  • 1
D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97
  • I tried to sue walkFileTree but it's also fail to work, I am not sure is it I use it in a wrong way or not ? – yumi Dec 29 '16 at 09:27
  • From your code I am not sure what you are trying to do with `walkfileTree` but you have to register subdirectories with the watcher manually. My understanding is they are not watched automatically upon creation. – D-Dᴙum Dec 29 '16 at 09:55
  • let me edit my question again to help you understand what I am doing – yumi Dec 29 '16 at 10:17
  • the way I code is following the reference link in my question – yumi Dec 29 '16 at 10:23