利用WatchService监控C盘根目录下的文件情况

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!
上一篇:windows 7 获取SYSTEM权限


下一篇:csdn第三名