不明白,怎么办
watcher.take();
知道要举报哪个目录?它是否报告其注册的每个目录?
dir.register(watcher...
如果我有多个观察者,他们是否只会报告针对他们注册的那些目录?
register()的返回值的用途是什么?看起来这里的描述中从未使用过它:http://docs.oracle.com/javase/tutorial/essential/io/notification.html
我们需要递归注册文件夹 F1 及其所有子文件夹 F11 和 F12。 官方文档本身也提供了解决方案。
这是您需要递归文件夹 F1 及其所有子文件夹到 watchService 的代码段:
/**
* Register the given directory and all its sub-directories with the WatchService.
*/
private void registerAll(final Path start) throws IOException {
// register directory and sub-directories
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
return FileVisitResult.CONTINUE;
}
});
}
如果有更多细节或者您不明白该代码段的用法。参考官方文档。下面提供了链接。
官方文档参考:
https://docs.oracle.com/javase/tutorial/essential/io/notification.html
https://docs.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java