#1.WatchService监控文件/文件目录变化
public static void main(String[] args) throws IOException, InterruptedException {
WatchService watcher = FileSystems.getDefault().newWatchService();
Paths.get("C:/code").register(watcher,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY);
while (true) {
WatchKey key = watcher.take();
for (WatchEvent<?> event: key.pollEvents()) {
System.out.println(event.context() + " comes to " + event.kind());
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
}