Class FileWatcher
java.lang.Object
cloud.opencode.base.io.file.FileWatcher
- All Implemented Interfaces:
AutoCloseable
File Watcher
文件监听器
Watches a directory for file changes using Java NIO WatchService. Supports monitoring create, modify, and delete events.
使用Java NIO WatchService监听目录的文件变化。 支持监控创建、修改和删除事件。
Features | 主要功能:
- Fluent API - 流式API
- Event filtering - 事件过滤
- Pattern matching - 模式匹配
- Virtual thread support - 虚拟线程支持
Usage Examples | 使用示例:
FileWatcher watcher = FileWatcher.watch(Path.of("/var/log"))
.filter("*.log")
.onModify(path -> System.out.println("Modified: " + path))
.onCreate(path -> System.out.println("Created: " + path));
watcher.start();
// ... do work ...
watcher.stop();
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Since:
- JDK 25, opencode-base-io V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Sets file name filter pattern 设置文件名过滤模式Gets the watch path 获取监听路径booleanChecks if watcher is running 检查监听器是否正在运行onAny(Consumer<FileWatcher.FileEvent> handler) Sets handler for any event 设置任意事件处理器Sets handler for create events 设置创建事件处理器Sets handler for delete events 设置删除事件处理器Sets handler for modify events 设置修改事件处理器voidstart()Starts watching 开始监听voidstop()Stops watching and releases OS file watch handles (inotify/kqueue).static FileWatcherCreates a file watcher for the given path string 为给定路径字符串创建文件监听器static FileWatcherCreates a file watcher for the given path 为给定路径创建文件监听器
-
Method Details
-
watch
Creates a file watcher for the given path 为给定路径创建文件监听器- Parameters:
path- the directory path to watch | 要监听的目录路径- Returns:
- file watcher | 文件监听器
-
watch
Creates a file watcher for the given path string 为给定路径字符串创建文件监听器- Parameters:
path- the directory path string | 目录路径字符串- Returns:
- file watcher | 文件监听器
-
filter
Sets file name filter pattern 设置文件名过滤模式- Parameters:
pattern- glob pattern (e.g., "*.log") | glob模式- Returns:
- this | 当前对象
-
onCreate
Sets handler for create events 设置创建事件处理器- Parameters:
handler- the handler | 处理器- Returns:
- this | 当前对象
-
onModify
Sets handler for modify events 设置修改事件处理器- Parameters:
handler- the handler | 处理器- Returns:
- this | 当前对象
-
onDelete
Sets handler for delete events 设置删除事件处理器- Parameters:
handler- the handler | 处理器- Returns:
- this | 当前对象
-
onAny
Sets handler for any event 设置任意事件处理器- Parameters:
handler- the handler | 处理器- Returns:
- this | 当前对象
-
start
public void start()Starts watching 开始监听 -
stop
public void stop()Stops watching and releases OS file watch handles (inotify/kqueue). 停止监听并释放操作系统文件监视句柄(inotify/kqueue)。 -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
isRunning
public boolean isRunning()Checks if watcher is running 检查监听器是否正在运行- Returns:
- true if running | 如果正在运行返回true
-
getWatchPath
-