When the file structure changes. I keep getting println
s for every single file that was changed. How do I make it so that I only get one println
for multiple changes in a 10 second period?
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
Path path = new File("C:\\Users\\myuser\\Desktop\\TestFolder").toPath();
path.register(
watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY);
WatchKey key;
while ((key = watchService.take()) != null) {
for (WatchEvent<?> event : key.pollEvents()) {
System.out.println(
"Event kind:" + event.kind() + ". File affected: " + event.context() + ".");
System.out.println("Something changed!");
Thread.sleep(10000);
System.out.println("Resuming..");
break;
}
key.reset();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}