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
WatchService
only looks at the file and folder modifications in the registeredPath
s.If you registered
Then adding a folder to
Desktop
will be seen. Changing a file inDesktop
will be seen. Adding a folder toDesktop/Subs
will also be seen, because the folderSubs
insideDesktop
was modified. However, modifying a file inDesktop/Subs
will not be seen because that path is not registered with the service.You can always register all the paths under
Desktop
recursivelyI have not tried on Linux but the following works on Windows