public class FileWatcher extends Object
To monitor files, you need to first implement the FileWatcher.EventHandler class that processes the events of
status changes on the monitored file directory.
Then, you should use the addWatch(java.nio.file.Path,ohos.utils.FileWatcher.EventHandler,boolean,java.nio.file.WatchEvent.Kind...) method to add the file directory to
be monitored and the associated event handler.
When a file in the specified directory is changed, the event handler will be triggered to process the event.
The following events defined in StandardWatchEventKinds are supported by FileWatcher:
StandardWatchEventKinds.ENTRY_CREATEStandardWatchEventKinds.ENTRY_DELETEStandardWatchEventKinds.ENTRY_MODIFY| 限定符和类型 | 类 | 说明 |
|---|---|---|
static interface |
FileWatcher.EventHandler |
Provides a handler for processing file events.
|
| 构造器 | 说明 |
|---|---|
FileWatcher() |
Default constructor used to create a
FileWatcher instance. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
addWatch(Path directory,
FileWatcher.EventHandler eventHandler,
boolean isRecursive,
WatchEvent.Kind<?>... events) |
Adds a watcher to monitor a specified file directory.
|
public FileWatcher()
FileWatcher instance.RuntimeException - Throws this exception if the file watcher service is not available.public void addWatch(Path directory, FileWatcher.EventHandler eventHandler, boolean isRecursive, WatchEvent.Kind<?>... events)
You can specify the file events to monitor. If no event is specified, all events on the file directory are monitored.
directory - Indicates the file directory to monitor.eventHandler - Indicates the event handler to add for the specified file directory.isRecursive - Specifies whether to recursively monitor the specified file directory.NullPointerException - Throws this exception if directory or eventHandler is null.RuntimeException - Throws this exception if adding the watcher fails.