Class FileWatcher


  • public final class FileWatcher
    extends java.lang.Object
    A FileWatcher can watch several files asynchronously.

    New watches are added with the addWatch(Path, Runnable) method, which specifies the task to execute when the file is modified.

    This class is thread-safe.

    • Constructor Summary

      Constructors 
      Constructor Description
      FileWatcher()
      Creates a new FileWatcher.
      FileWatcher​(java.util.function.Consumer<java.lang.Exception> exceptionHandler)
      Creates a new FileWatcher.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addWatch​(java.io.File file, java.lang.Runnable changeHandler)
      Watches a file, if not already watched by this FileWatcher.
      void addWatch​(java.nio.file.Path file, java.lang.Runnable changeHandler)
      Watches a file, if not already watched by this FileWatcher.
      static FileWatcher defaultInstance()
      Gets the default, global instance of FileWatcher.
      void removeWatch​(java.io.File file)
      Stops watching a file.
      void removeWatch​(java.nio.file.Path file)
      Stops watching a file.
      void setWatch​(java.io.File file, java.lang.Runnable changeHandler)
      Watches a file.
      void setWatch​(java.nio.file.Path file, java.lang.Runnable changeHandler)
      Watches a file.
      void stop()
      Stops this FileWatcher.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileWatcher

        public FileWatcher()
        Creates a new FileWatcher. The watcher is immediately functional, there is no need (and no way, actually) to start it manually.
      • FileWatcher

        public FileWatcher​(java.util.function.Consumer<java.lang.Exception> exceptionHandler)
        Creates a new FileWatcher. The watcher is immediately functional, there is no need (and no way, actually) to start it manually.
    • Method Detail

      • defaultInstance

        public static FileWatcher defaultInstance()
        Gets the default, global instance of FileWatcher.
        Returns:
        the default FileWatcher
      • addWatch

        public void addWatch​(java.io.File file,
                             java.lang.Runnable changeHandler)
                      throws java.io.IOException
        Watches a file, if not already watched by this FileWatcher.
        Parameters:
        file - the file to watch
        changeHandler - the handler to call when the file is modified
        Throws:
        java.io.IOException
      • addWatch

        public void addWatch​(java.nio.file.Path file,
                             java.lang.Runnable changeHandler)
                      throws java.io.IOException
        Watches a file, if not already watched by this FileWatcher.
        Parameters:
        file - the file to watch
        changeHandler - the handler to call when the file is modified
        Throws:
        java.io.IOException
      • setWatch

        public void setWatch​(java.io.File file,
                             java.lang.Runnable changeHandler)
                      throws java.io.IOException
        Watches a file. If the file is already watched by this FileWatcher, its changeHandler is replaced.
        Parameters:
        file - the file to watch
        changeHandler - the handler to call when the file is modified
        Throws:
        java.io.IOException
      • setWatch

        public void setWatch​(java.nio.file.Path file,
                             java.lang.Runnable changeHandler)
                      throws java.io.IOException
        Watches a file. If the file is already watched by this FileWatcher, its changeHandler is replaced.
        Parameters:
        file - the file to watch
        changeHandler - the handler to call when the file is modified
        Throws:
        java.io.IOException
      • removeWatch

        public void removeWatch​(java.io.File file)
        Stops watching a file.
        Parameters:
        file - the file to stop watching
      • removeWatch

        public void removeWatch​(java.nio.file.Path file)
        Stops watching a file.
        Parameters:
        file - the file to stop watching
      • stop

        public void stop()
                  throws java.io.IOException
        Stops this FileWatcher. The underlying ressources (ie the WatchServices) are closed, and the file modification handlers won't be called anymore.
        Throws:
        java.io.IOException