Skip to content

Instantly share code, notes, and snippets.

@dangnhdev
Last active February 4, 2016 15:19
Show Gist options
  • Save dangnhdev/a7fe899e984c34995b05 to your computer and use it in GitHub Desktop.
Save dangnhdev/a7fe899e984c34995b05 to your computer and use it in GitHub Desktop.
import java.nio.file.*;
import static java.nio.file.StandardWatchEventKinds.*;
/*
Author: NguyenHaiDang@ActiveStudy
*/
public class Watcher {
public static void main(String[] args) {
Path this_dir = Paths.get(".");
System.out.println("Now watching the current directory ...");
try {
WatchService watcher = this_dir.getFileSystem().newWatchService();
this_dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
WatchKey watckKey = watcher.take();
while (watckKey != null) {
for (WatchEvent<?> event : watckKey.pollEvents()) {
System.out.println(event.kind().name() +"\t" + event.context().toString());
watckKey.reset();
watckKey = watcher.take();
}
}
} catch (Exception e) {
System.out.println("Error: " + e.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment