This example reports to the command line any changes to the contents of your Downloads and Documents folders. It tracks the entire Downloads and Documents hierarchies.
This example is based on Java 7’s Watch Service Tutorial here.
Demo.java
package com.barbarysoftware.watchservice;
import static com.barbarysoftware.watchservice.StandardWatchEventKind.*;
import java.io.File;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException, InterruptedException {
final WatchService watcher = WatchService.newWatchService();
final String home = System.getProperty("user.home");
final WatchableFile file1 = new WatchableFile(new File(home + "/Downloads"));
final WatchableFile file2 = new WatchableFile(new File(home + "/Documents"));
file1.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
file2.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
Runnable runnable = createRunnable(watcher);
final Thread consumer = new Thread(runnable);
consumer.start();
System.out.println("Watching for changes for 1 minute...");
Thread.sleep(60000);
consumer.interrupt();
watcher.close();
}
private static Runnable createRunnable(final WatchService watcher) {
return new Runnable() {
public void run() {
for (; ;) {
// wait for key to be signaled
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == OVERFLOW) {
continue;
}
// The filename is the context of the event.
@SuppressWarnings({"unchecked"}) WatchEvent<File> ev = (WatchEvent<File>) event;
System.out.println("detected file system event: " + ev.context() + " " + kind);
}
// Reset the key -- this step is critical to receive further watch events.
boolean valid = key.reset();
if (!valid) {
break;
}
}
}
};
}
}