DirectoryUpdateMonitor { public DirectoryUpdateMonitor(List<File> directories, long pollFrequency, boolean syncDeletes) { monitor = new FileAlterationMonitor(pollFrequency); for (File watchDir : directories) { if (watchDir.exists()) { FileAlterationObserver observer; if (watchDir.isDirectory()) { observer = new FileAlterationObserver(watchDir); } else { final File file = watchDir; observer = new FileAlterationObserver(watchDir.getParentFile(), new FileFilter() { @Override public boolean accept(File pathname) { return (file.equals(pathname)); } }); } observer.addListener(new DirectoryListener(syncDeletes)); monitor.addObserver(observer); } else { throw new RuntimeException("Path " + watchDir.getAbsolutePath() + " does not exist"); } } } DirectoryUpdateMonitor(List<File> directories,
                                  long pollFrequency,
                                  boolean syncDeletes); void startMonitor(); void stopMonitor();  }
@Test public void testDirectoryUpdateMonitor() throws Exception { List<File> dirs = new ArrayList<File>(); dirs.add(tempDir); DirectoryUpdateMonitor monitor = new DirectoryUpdateMonitor(dirs, 100, true); monitor.startMonitor(); File tempFile = File.createTempFile("temp", "file", tempDir); checkFileInChangedList(tempFile); FileWriter writer = new FileWriter(tempFile); writer.write("test"); writer.close(); checkFileInChangedList(tempFile); tempFile.delete(); checkFileInChangedList(tempFile); monitor.stopMonitor(); }