In order to respond to file system changes, a {@link DirectoryWatcherSubscriber} should be provided to theDirectoryWatcher. Subscribers can respond to 3 types of events:
watcher.subscribe(new DirectoryWatcherSubscriber() { public void entryCreated(DirectoryWatcher watcher, Path file) { // ... } public void entryModified(DirectoryWatcher watcher, Path file) { // ... } public void entryDeleted(DirectoryWatcher watcher, Path file) { // ... } });
It is not mandatory to respond to all types of events. Simple override the methods corresponding to the events you'd like to track. As a convenience, you can also use a {@link DirectoryChangedSubscriber}, which will notify you of all events.
watcher.subscribe(new DirectoryChangedSubscriber() { public void directoryChanged(DirectoryWatcher watcher, Path path) { // ... } });
By default, the DirectoryWatcher will notify subscribers of every change in every file under its base path. Often times, this is not desired. You can fine tune which path changes you'd like to respond to through Ant style path filtering
All events must satisfy all "include" conditions, and no "exclude" conditions, before being sent to subscribers. You can use {@link DirectoryWatcher#shouldTrack} to check if a particularpath is being watched.
Some (inexhaustive) Examples:
watcher.include("file"); watcher.shouldTrack(Paths.get("file")); // true watcher.shouldTrack(Paths.get("file.json")); // false watcher.shouldTrack(Paths.get("foo/file")); // false watcher.shouldTrack(Paths.get("foo/file.json")); // false watcher.shouldTrack(Paths.get("foo/bar/file.json")); // false
watcher.include("*.json"); watcher.shouldTrack(Paths.get("file")); // false watcher.shouldTrack(Paths.get("file.json")); // true watcher.shouldTrack(Paths.get("foo/file")); // false watcher.shouldTrack(Paths.get("foo/file.json")); // false watcher.shouldTrack(Paths.get("foo/bar/file.json")); // false
watcher.include("**/file.json"); watcher.shouldTrack(Paths.get("file")); // false watcher.shouldTrack(Paths.get("file.json")); // true watcher.shouldTrack(Paths.get("foo/file")); // false watcher.shouldTrack(Paths.get("foo/file.json")); // true watcher.shouldTrack(Paths.get("foo/bar/file.json")); // true
watcher.exclude("foo/**"); watcher.shouldTrack(Paths.get("file")); // true watcher.shouldTrack(Paths.get("file.json")); // true watcher.shouldTrack(Paths.get("foo/file")); // false watcher.shouldTrack(Paths.get("foo/file.json")); // false watcher.shouldTrack(Paths.get("foo/bar/file.json")); // false
watcher.exclude("foo/*"); watcher.shouldTrack(Paths.get("file")); // true watcher.shouldTrack(Paths.get("file.json")); // true watcher.shouldTrack(Paths.get("foo/file")); // false watcher.shouldTrack(Paths.get("foo/file.json")); // false watcher.shouldTrack(Paths.get("foo/bar/file.json")); // true
Events from files are relatively straight forward. However, directories are a little trickier, as actions taken on its contents may trigger events propagating from the directory itself (depending on operating system). For example, adding a file to a watched directory may cause 2 events (a File Created event and a Directory Modified event). Therefore, if you wish to completely ignore all changes within a directory, it may also be necessary to exclude changes to the directory itself.
watcher.exclude("foo"); watcher.exclude("foo/**");
In order to make this library as performant as possible, there is no functionality currently available to provide information as to whether the deleted entry was originally a directory or a file. If this functionality is required you should track the file structure yourself.
@author Daryl Teo
|
|