Class FileWatcher

java.lang.Object
cloud.opencode.base.io.file.FileWatcher
All Implemented Interfaces:
AutoCloseable

public final class FileWatcher extends Object implements 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:
  • Method Details

    • watch

      public static FileWatcher watch(Path path)
      Creates a file watcher for the given path 为给定路径创建文件监听器
      Parameters:
      path - the directory path to watch | 要监听的目录路径
      Returns:
      file watcher | 文件监听器
    • watch

      public static FileWatcher watch(String path)
      Creates a file watcher for the given path string 为给定路径字符串创建文件监听器
      Parameters:
      path - the directory path string | 目录路径字符串
      Returns:
      file watcher | 文件监听器
    • filter

      public FileWatcher filter(String pattern)
      Sets file name filter pattern 设置文件名过滤模式
      Parameters:
      pattern - glob pattern (e.g., "*.log") | glob模式
      Returns:
      this | 当前对象
    • onCreate

      public FileWatcher onCreate(Consumer<Path> handler)
      Sets handler for create events 设置创建事件处理器
      Parameters:
      handler - the handler | 处理器
      Returns:
      this | 当前对象
    • onModify

      public FileWatcher onModify(Consumer<Path> handler)
      Sets handler for modify events 设置修改事件处理器
      Parameters:
      handler - the handler | 处理器
      Returns:
      this | 当前对象
    • onDelete

      public FileWatcher onDelete(Consumer<Path> handler)
      Sets handler for delete events 设置删除事件处理器
      Parameters:
      handler - the handler | 处理器
      Returns:
      this | 当前对象
    • onAny

      public FileWatcher onAny(Consumer<FileWatcher.FileEvent> handler)
      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:
      close in interface AutoCloseable
    • isRunning

      public boolean isRunning()
      Checks if watcher is running 检查监听器是否正在运行
      Returns:
      true if running | 如果正在运行返回true
    • getWatchPath

      public Path getWatchPath()
      Gets the watch path 获取监听路径
      Returns:
      watch path | 监听路径