Java tutorial
import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardWatchEventKinds; import java.nio.file.WatchEvent; import java.nio.file.WatchEvent.Kind; import java.nio.file.WatchKey; import java.nio.file.WatchService; class MyWatch { public void watchRNDir(Path path) throws IOException, InterruptedException { try (WatchService watchService = FileSystems.getDefault().newWatchService()) { path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE); while (true) { // retrieve and remove the next watch key final WatchKey key = watchService.take(); for (WatchEvent<?> watchEvent : key.pollEvents()) { final Kind<?> kind = watchEvent.kind(); if (kind == StandardWatchEventKinds.OVERFLOW) { continue; } final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent; final Path filename = watchEventPath.context(); System.out.println(kind + " -> " + filename); } boolean valid = key.reset(); if (!valid) { break; } } } } } public class Main { public static void main(String[] args) { final Path path = Paths.get("C:/tutorial"); MyWatch watch = new MyWatch(); try { watch.watchRNDir(path); } catch (IOException | InterruptedException ex) { System.err.println(ex); } } }