Class DirWatcher

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class DirWatcher
    extends Object
    implements Closeable
    A dir watcher uses a native watch service and watch directories for any change. Each listener is registered to be listening to individual directories, but can be registered for more than one. The listener will then be notified of changes to the content of the directory. Including:
    • Files, sub-directories or symlinks being created
    • Files, sub-directories or symlinks being deleted
    • Files being written to. This MAY cause multiple events, as each event is triggered by file-system writes.
    • When a file is moved into or out from the directory. Handled as a FileEvent.CREATED or FileEvent.DELETED event respectively.
    It does NOT notify on:
    • The listened to directory being deleted itself.
    • Changes to metadata and permissions
    • Constructor Detail

      • DirWatcher

        public DirWatcher()
        Create a FileWatcher with default watch service.
      • DirWatcher

        public DirWatcher​(WatchService watchService)
        Create a FileWatcher using the provided watch service.
        Parameters:
        watchService - Watcher service to use.
    • Method Detail

      • addWatcher

        public DirWatcher addWatcher​(Path dir,
                                     FileEventListener listener)
        Start watching file path and notify watcher for updates on that file.
        Parameters:
        dir - The file path to watch.
        listener - The listener to be notified.
        Returns:
        The watcher.
      • weakAddWatcher

        public DirWatcher weakAddWatcher​(Path dir,
                                         FileEventListener listener)
        Start watching file path and notify watcher for updates on that file.
        Parameters:
        dir - The file path to watch.
        listener - The listener to be notified.
        Returns:
        The watcher.
      • removeWatcher

        public boolean removeWatcher​(Path dir,
                                     FileEventListener listener)
        Remove a watcher from the list of listeners.
        Parameters:
        dir - The file path to no longer watch.
        listener - The listener to be removed.
        Returns:
        True if the listener was removed from the list.
      • removeWatcher

        public boolean removeWatcher​(FileEventListener listener)
        Remove a watcher from the list of listeners.
        Parameters:
        listener - The listener to be removed.
        Returns:
        True if the listener was removed from the list.