Class FileWatcher

java.lang.Object
com.electronwill.nightconfig.core.file.FileWatcher

public final class FileWatcher extends 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 Details

    • 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(Consumer<Exception> exceptionHandler)
      Creates a new FileWatcher. The watcher is immediately functional, there is no need (and no way, actually) to start it manually.
  • Method Details

    • defaultInstance

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

      public void addWatch(File file, Runnable changeHandler) throws 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:
      IOException
    • addWatch

      public void addWatch(Path file, Runnable changeHandler) throws 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:
      IOException
    • setWatch

      public void setWatch(File file, Runnable changeHandler) throws 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:
      IOException
    • setWatch

      public void setWatch(Path file, Runnable changeHandler) throws 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:
      IOException
    • removeWatch

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

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

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