Last active
February 4, 2016 15:19
-
-
Save dangnhdev/a7fe899e984c34995b05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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