1.依赖包
<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-core</artifactId>
            <version>5.5.7</version>
</dependency>

2.实现代码
File file = FileUtil.file("example.properties");
//这里只监听文件或目录的修改事件
WatchMonitor watchMonitor = WatchMonitor.create(file, WatchMonitor.ENTRY_MODIFY);
watchMonitor.setWatcher(new Watcher(){
    @Override
    public void onCreate(WatchEvent<?> event, Path currentPath) {
        Object obj = event.context();
        Console.log("创建:{}-> {}", currentPath, obj);
    }

    @Override
    public void onModify(WatchEvent<?> event, Path currentPath) {
        Object obj = event.context();
        Console.log("修改:{}-> {}", currentPath, obj);
    }

    @Override
    public void onDelete(WatchEvent<?> event, Path currentPath) {
        Object obj = event.context();
        Console.log("删除:{}-> {}", currentPath, obj);
    }

    @Override
    public void onOverflow(WatchEvent<?> event, Path currentPath) {
        Object obj = event.context();
        Console.log("Overflow:{}-> {}", currentPath, obj);
    }
});

//设置监听目录的最大深入,目录层级大于制定层级的变更将不被监听,默认只监听当前层级目录
watchMonitor.setMaxDepth(3);
//启动监听
watchMonitor.start();

  

相关文章:

  • 2021-11-02
  • 2022-12-23
  • 2022-12-23
  • 2021-08-15
  • 2022-12-23
  • 2021-09-28
  • 2021-09-13
猜你喜欢
  • 2021-06-02
  • 2022-12-23
  • 2022-02-16
  • 2022-12-23
  • 2021-10-11
  • 2022-12-23
相关资源
相似解决方案