import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.List;
public class FileWatchServiceDemo {
public static void main(String[] args) throws IOException, InterruptedException {
WatchService watchService = FileSystems.getDefault().newWatchService();
String filePath = "D:/aa";
Paths.get(filePath).register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
while(true){
WatchKey key = watchService.take();
List<WatchEvent<?>> watchEvents = key.pollEvents();
for (WatchEvent<?> event : watchEvents) {
if(StandardWatchEventKinds.ENTRY_CREATE == event.kind()){
System.out.println("创建:[" + filePath + "/" + event.context() + "]");
}
if(StandardWatchEventKinds.ENTRY_MODIFY == event.kind()){
System.out.println("修改:[" + filePath + "/" + event.context() + "]");
}
if(StandardWatchEventKinds.ENTRY_DELETE == event.kind()){
System.out.println("删除:[" + filePath + "/" + event.context() + "]");
}
}
key.reset();
}
}
}
StandardWatchEventKinds.ENTRY_CREATE //创建 StandardWatchEventKinds.ENTRY_MODIFY //修改 StandardWatchEventKinds.ENTRY_DELETE //删除
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有