`file.listfiles()` 返回结果未及时更新,是因为 `file` 对象是不可变的快照,不会自动感知磁盘变化;需每次调用时重新创建 `file` 实例才能获取当前目录真实状态。
在基于 WatchService 的文件监控场景中,一个常见误区是:复用初始化时创建的 File 对象(如 this.myDir),并在循环中反复调用其 listFiles() 方法。由于 java.io.File 本质上是一个路径引用(path abstraction),而非实时文件系统句柄,它不维护与底层目录状态的同步——即使文件已被创建、删除或重命名,原有 File 实例的 listFiles() 调用仍可能返回过期缓存结果(尤其在某些 JVM 或文件系统实现下,或受操作系统缓存影响)。
✅ 正确做法是:每次需要枚举当前目录内容时,动态构造新的 File 实例,确保路径解析和文件列表操作基于最新上下文:
@Async
public void registerWatchService() throws IOException, InterruptedException {
try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
Path path = Paths.get(myPath);
path.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY);
WatchKey key;
while ((key = watchService.take()) != null) {
for (WatchEvent event : key.pollEvents()) {
System.out.println("Event: " + event.kind() + ", File: " + event.context());
}
key.reset();
// ✅ 关键修复:每次重新创建 File 实例,避免状态陈旧
File freshDir = new File(myPath);
File[] currentFiles = freshDir.listFiles();
if (currentFiles != null) {
System.out.println("Current files: " + Arrays.toString(currentFiles));
} else {
System.out.println("Directory is empty or inaccessible.");
}
}
}
}
⚠️ 注意事项: