问题描述
我有一个服务,用于检查文件夹中的ZIP文件,
while(true) {
WatchKey wk = watchService.take();
for (WatchEvent<?> event : wk.pollEvents()) {
Kind<?> eventKind = event.kind();
Path dir = (Path)wk.watchable();
Path eventPath = (Path) event.context();
Path fullPath = dir.resolve(eventPath);
fireAction(eventKind,fullPath);
}
wk.reset();
}
如果看到的话,每次执行操作时都会调用 fireAction()方法,下面是该方法
public void fireAction(Kind<?> eventKind,Path eventPath) {
synchronized (listeners) {
if (eventKind == StandardWatchEventKinds.ENTRY_MODIFY) {
fileModified(this,eventPath);
} else if (eventKind == StandardWatchEventKinds.ENTRY_DELETE) {
fileDeleted(this,eventPath);
} else if (eventKind == StandardWatchEventKinds.ENTRY_CREATE) {
fileCreated(this,eventPath);
}
}
}
因此,当文件夹为空并且我第一次将ZIP保留在文件夹中时,将调用fileCreate()方法,但未完成,并且fileModify()方法被触发,而fileModify()被触发2次。当我从文件夹中删除ZIP时,它可以正常工作,但是当我再次保留ZIP时,它可以正常工作。
因此只有在第一次出现问题的情况下。请建议,这是我尝试过的
- take()和pollEvents()之间的Thread.sleep(3000),位于for循环之外。
- 使用.lastModified()来查看文件是否已更新,但是我不确定是否正确执行了操作,还有其他方法吗?请建议
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)