使用WatchService监控文件变化
生活随笔
收集整理的這篇文章主要介紹了
使用WatchService监控文件变化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package com.nanhao.FileWatch;import java.nio.file.*;public class FileWatch {public static void main(String []args) throws Exception{WatchService watchService = FileSystems.getDefault().newWatchService();//為C盤根目錄注冊監聽Paths.get("c:/").register(watchService,StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_MODIFY);while(true){WatchKey watchKey = watchService.take();for(WatchEvent<?>event : watchKey.pollEvents()){System.out.println(event.context()+"文件發生了"+event.kind()+"事件");}//重設WatchKeyboolean valid = watchKey.reset();if(!valid){break;}}}}
?
總結
以上是生活随笔為你收集整理的使用WatchService监控文件变化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IDEA中实用的快捷方式
- 下一篇: @FunctionalInterface