import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class WatchMe {
public static void main(String[] args) {
Path currentDirectory = Paths.get(System.getProperty("java.io.tmpdir"));
try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
WatchKey watchKey = currentDirectory.register(watchService, ENTRY_CREATE, ENTRY_DELETE,
ENTRY_MODIFY);
do {
watchKey = watchService.take();
for (WatchEvent<?> event : watchKey.pollEvents()) {
System.out.println(String.format("%s event on %s",
event.kind(), event.context()));
}
} while (watchKey.reset());
} catch (Exception e) {
}
}
}