alize observer.init(); ... // invoke as required observer.checkAndNotify(); ... observer.checkAndNotify(); ... // finished observer.finish(); Alternatively, register the oberver(s) with a {@link FileAlterationMonitor}, which creates a new thread, invoking the observer at the specified interval:
long interval = ... FileAlterationMonitor monitor = new FileAlterationMonitor(interval); monitor.addObserver(observer); monitor.start(); ... monitor.stop();
File Filters
This implementation can monitor portions of the file system by using {@link FileFilter}s to observe only the files and/or directories that are of interest. This makes it more efficient and reduces the noise from
unwanted file system events.
Commons IO has a good range of useful, ready made File Filter implementations for this purpose.
For example, to only observe 1) visible directories and 2) files with a ".java" suffix in a root directory called "src" you could set up a {@link FileAlterationObserver} in the followingway:
// Create a FileFilter IOFileFilter directories = FileFilterUtils.and( FileFilterUtils.directoryFileFilter(), HiddenFileFilter.VISIBLE); IOFileFilter files = FileFilterUtils.and( FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter(".java")); IOFileFilter filter = FileFilterUtils.or(directories, files); // Create the File system observer and register File Listeners FileAlterationObserver observer = new FileAlterationObserver(new File("src"), filter); observer.addListener(...); observer.addListener(...);
FileEntry
{@link FileEntry} represents the state of a file or directory, capturing{@link File} attributes at a point in time. Custom implementations of{@link FileEntry} can be used to capture additional properties that thebasic implementation does not support. The {@link FileEntry#refresh(File)}method is used to determine if a file or directory has changed since the last check and stores the current state of the {@link File}'s properties.
@see FileAlterationListener
@see FileAlterationMonitor
@version $Id: FileAlterationObserver.java 1415850 2012-11-30 20:51:39Z ggregory $
@since 2.0