Class FileWatcher
- java.lang.Object
-
- com.electronwill.nightconfig.core.file.FileWatcher
-
public final class FileWatcher extends java.lang.ObjectA 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 voidaddWatch(java.io.File file, java.lang.Runnable changeHandler)Watches a file, if not already watched by this FileWatcher.voidaddWatch(java.nio.file.Path file, java.lang.Runnable changeHandler)Watches a file, if not already watched by this FileWatcher.static FileWatcherdefaultInstance()Gets the default, global instance of FileWatcher.voidremoveWatch(java.io.File file)Stops watching a file.voidremoveWatch(java.nio.file.Path file)Stops watching a file.voidsetWatch(java.io.File file, java.lang.Runnable changeHandler)Watches a file.voidsetWatch(java.nio.file.Path file, java.lang.Runnable changeHandler)Watches a file.voidstop()Stops this FileWatcher.
-
-
-
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.IOExceptionWatches a file, if not already watched by this FileWatcher.- Parameters:
file- the file to watchchangeHandler- 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.IOExceptionWatches a file, if not already watched by this FileWatcher.- Parameters:
file- the file to watchchangeHandler- 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.IOExceptionWatches a file. If the file is already watched by this FileWatcher, its changeHandler is replaced.- Parameters:
file- the file to watchchangeHandler- 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.IOExceptionWatches a file. If the file is already watched by this FileWatcher, its changeHandler is replaced.- Parameters:
file- the file to watchchangeHandler- 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.IOExceptionStops this FileWatcher. The underlying ressources (ie the WatchServices) are closed, and the file modification handlers won't be called anymore.- Throws:
java.io.IOException
-
-