public static void main(String[] args) throws IOException, InterruptedException {
        WatchService watchService = FileSystems.getDefault().newWatchService();
        Paths.get("c:/").register(watchService, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_MODIFY);
        while(true){
            WatchKey key = watchService.take();
            for (WatchEvent<?> event :
                    key.pollEvents()) {
                System.out.println(event.context()+" File occur"+event.kind()+"event!");
            }
            boolean valid = key.reset();
            if(!valid){
                break;
            }
        }
    }

 当在C盘根目录下创建文件和删除文件时:

新建文件夹 File occurENTRY_CREATEevent!
新建文件夹 File occurENTRY_DELETEevent!

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-29
  • 2022-12-23
  • 2021-11-26
  • 2021-07-24
  • 2022-12-23
猜你喜欢
  • 2021-07-15
  • 2022-12-23
  • 2021-07-16
  • 2021-12-27
  • 2021-11-10
  • 2022-02-02
相关资源
相似解决方案