Example: I have my desktop with a subfolder "Desktop/sub"
if only Desktop is registered and once started the WatchService I modify the content of a subfolder.. should it trigger an ENTRY_MODIFY or not? I tried and it does not. It works only for modifying a file within the registered folder (Desktop in this case).
Just asking this question because in a mock exam for OCPJP7 it says that actually even modifying a subfolder such as "Desktop/sub" for example removing a file within "Desktop/sub" it should trigger the event.
However again, I have tried it out and it does not work.
Here is the code:
WatchKey key = ws.poll(5, TimeUnit.SECONDS);
if (key ==null)
{
continue;
}
List<WatchEvent<?>> lista = key.pollEvents();
for (WatchEvent<?> event : lista)
{
switch(event.kind().toString())
{
case "ENTRY_CREATE": System.out.println("There has been a creation over here: "+event.context());break;
case "ENTRY_MODIFY": System.out.println("There has been a modification over here: "+event.context());break;
case "ENTRY_DELETE": System.out.println("There has been a deletion over here: "+event.context());
}
key.reset();
}
Thanks in advance.
The
WatchServiceonly looks at the file and folder modifications in the registeredPaths.If you registered
Then adding a folder to
Desktopwill be seen. Changing a file inDesktopwill be seen. Adding a folder toDesktop/Subswill also be seen, because the folderSubsinsideDesktopwas modified. However, modifying a file inDesktop/Subswill not be seen because that path is not registered with the service.You can always register all the paths under
DesktoprecursivelyI have not tried on Linux but the following works on Windows