Skip to main content

Posts

Showing posts with the label watch service

Watching A Directory For Change

import java.io.IOException; import java.nio.file.*; public class ListenDirectory { public static void main(String[] args) { Path path = Paths.get(args[0]); WatchService watchService = null; try { watchService = path.getFileSystem().newWatchService(); path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_CREATE); } catch (IOException e) { e.printStackTrace(); } WatchKey key = null; try{ //infinite loop while((key = watchService.take())!=null){// blocks until a key is available // iterate for each event for(WatchEvent<?> event:key.pollEvents()){ switch(event.kind().name()){ case "OVERFLOW": ...