public final class FileWatcher
extends java.lang.Object
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.
| Modifier and Type | Class and Description |
|---|---|
static class |
FileWatcher.WatchingException |
| Constructor and Description |
|---|
FileWatcher()
Creates a new FileWatcher with a default exception handler and a default debounce time.
|
FileWatcher(java.util.function.Consumer<java.lang.Exception> exceptionHandler)
Creates a new FileWatcher with the default debounce time and the given exception handler.
|
FileWatcher(java.time.Duration debounceTime)
Creates a new FileWatcher with a default exception handler.
|
FileWatcher(java.time.Duration debounceTime,
java.util.function.Consumer<java.lang.Throwable> exceptionHandler)
Creates a new FileWatcher with the given exception handler.
|
| Modifier and Type | Method and 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.
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
addWatchFuture(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.
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
removeWatchFuture(java.nio.file.Path 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.
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
setWatchFuture(java.nio.file.Path file,
java.lang.Runnable changeHandler) |
void |
stop()
Stops this FileWatcher.
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
stopFuture() |
public FileWatcher()
(e) -> e.printStackTrace();
The default debounce time is unspecified and may change in the future, but is typically
around 1 second or less.public FileWatcher(java.util.function.Consumer<java.lang.Exception> exceptionHandler)
The default debounce time is unspecified and may change in the future, but is typically around 1 second or less.
public FileWatcher(java.time.Duration debounceTime)
(e) -> e.printStackTrace();
debounceTime - hwo much time to wait after each modification of a file before triggering the change handlerpublic FileWatcher(java.time.Duration debounceTime,
java.util.function.Consumer<java.lang.Throwable> exceptionHandler)
debounceTime - delay between each call of the file's changeHandler.exceptionHandler - called when an exception occurs during the handling of file eventspublic static FileWatcher defaultInstance()
public void addWatch(java.io.File file,
java.lang.Runnable changeHandler)
NOTE: This method may return before the handler is set up.
Prefer to use addWatchFuture(Path, Runnable).
file - the file to watchchangeHandler - the handler to call when the file is modifiedpublic void addWatch(java.nio.file.Path file,
java.lang.Runnable changeHandler)
NOTE: This method may return before the handler is set up.
Prefer to use addWatchFuture(Path, Runnable).
file - the file to watchchangeHandler - the handler to call when the file is modifiedpublic java.util.concurrent.CompletableFuture<java.lang.Void> addWatchFuture(java.nio.file.Path file,
java.lang.Runnable changeHandler)
This method returns a CompletableFuture that is completed when the
handler is registered and ready to be notified of file events.
FileWatcher watcher = FileWatcher.defaultInstance();
CompletableFuture<Void> f = watcher.addWatchFuture(file, handler);
f.join();
Watching multiple files and waiting for the handlers to be ready,
with a timeout of 1 second:
FileWatcher watcher = FileWatcher.defaultInstance();
List<CompletableFuture<Void>> futures = new ArrayList<>();
for (Path path : filesToWatch) {
futures.add(watcher.addWatchFuture(file, () -> {
// react to file creation or modification
}));
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).get(1, TimeUnit.SECONDS);
file - the file to watchchangeHandler - the handler to call when the file is modifiedpublic void setWatch(java.io.File file,
java.lang.Runnable changeHandler)
NOTE: This method may return before the handler is set up.
Prefer to use setWatchFuture(Path, Runnable).
file - the file to watchchangeHandler - the handler to call when the file is modifiedpublic void setWatch(java.nio.file.Path file,
java.lang.Runnable changeHandler)
NOTE: This method may return before the handler is set up.
Prefer to use setWatchFuture(Path, Runnable).
file - the file to watchchangeHandler - the handler to call when the file is modifiedpublic java.util.concurrent.CompletableFuture<java.lang.Void> setWatchFuture(java.nio.file.Path file,
java.lang.Runnable changeHandler)
public void removeWatch(java.io.File file)
NOTE: This method may return before the handler is removed.
Prefer to use setWatchFuture(Path, Runnable).
file - the file to stop watchingpublic void removeWatch(java.nio.file.Path file)
NOTE: This method may return before the handler is removed.
Prefer to use setWatchFuture(Path, Runnable).
file - the file to stop watchingpublic java.util.concurrent.CompletableFuture<java.lang.Void> removeWatchFuture(java.nio.file.Path file)
public void stop()
NOTE: This method may return before the FileWatcher is completely stopped.
Prefer to use stopFuture().
public java.util.concurrent.CompletableFuture<java.lang.Void> stopFuture()