2
我试图监视更改的文件夹。此文件夹包含我不想看的子文件夹。不幸的是,WatchService通知这些子文件夹的改变了我。我想这是因为这些文件夹更新的最后更改日期。WatchService排除的文件夹
于是,我就exlude他们:
WatchService service = FileSystems.getDefault().newWatchService();
workPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
try {
WatchKey watchKey = watchService.take();
for (WatchEvent<?> event : watchKey.pollEvents()) {
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path fileName = ev.context();
if (!Files.isDirectory(fileName)) {
logger.log(LogLevel.DEBUG, this.getClass().getSimpleName(),
"Change registered in " + fileName + " directory. Checking configurations.");
/* do stuff */
}
}
if (!watchKey.reset()) {
break;
}
} catch (InterruptedException e) {
return;
}
这不工作,虽然。上下文的结果路径是相对的,并且Files.isDirectory()
无法确定它是目录还是文件。
有没有办法排除子文件夹?
谢谢。解决帮助。 'Path fileName = workPath.resolve(ev.context())' –