Well I've tried the code showcased in the link you posted, doing the same things you're trying to do:
In my c:\temp directory I create programatically a c:\temp\a dir and then a c:\temp\a\b directory:
File startDir = new File("c:\temp");
if(!startDir.exists()) {
startDir.mkdir();
}
File aDir = new File("c:\\temp\\a");
File bDir = new File("c:\\temp\\a\\b");
if(!aDir.exists()) {
aDir.mkdir();
}
if(!bDir.exists()) {
bDir.mkdir();
}
Then I add watchers to the "a" and "b" directories:
public static void watch(final File dir,final WatchService watcher) {
Path path = dir.toPath();
try {
final WatchKey bDirWatchKey = path.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
new Thread(new Runnable() {
public void run() {
System.out.println("Watching: "+dir.getName());
while(true) {
try {Thread.sleep(1000);} catch (InterruptedException e) {}
List<WatchEvent<?>> events = bDirWatchKey.pollEvents();
for(WatchEvent<?> event:events) {
System.out.println(dir.getName()+" event: #"+event.count()+","+event.kind()+" File="+event.context());
}
}
}
}).start();
} catch (IOException x) {
x.printStackTrace();
}
}
This works ok, if I modify files in "a" or "b" I get the corresponding console output.
It's true that with Windows Explorer (on a Windowx XP machine) I cannot delete a watched directory (it tells me I don't have access rights). I can however delete it with other tools such as Total Commander. I can even delete then from the Windows command line with rd c:\temp\a\b
. I think this is more of an issue with Windows Explorer than with Java...