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/abc
inside 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