java - 当文件改变时,我读取文件,文件的内容总是一样的?
问题描述
我使用jdk1.8提供的watchevent来监听文件更改事件,但是当事件发生时,我读取了文件,但是文件的内容总是一样的? 监视文件更改事件代码:
final WatchKey key = watcher.take();
for (WatchEvent<?> event : key.pollEvents()) {
// get event type
final WatchEvent.Kind<?> kind = event.kind();
// get file name
@SuppressWarnings("unchecked") final WatchEvent<Path> pathWatchEvent = (WatchEvent<Path>) event;
final Path fileName = pathWatchEvent.context();
if (kind == ENTRY_DELETE) {
log.info(String.format("%s文件被删除",fileName));
} else if (kind == ENTRY_MODIFY) {
log.info(fileName.toString());
log.info("ENTRY_MODIFY");
if (Constant.ERROR_LOG.equals(fileName.toString())) {
String filePath = "\\\\192.168.160.128\\share\\error_log";
int totalLine = FileUtils.countFileTotalLine(filePath);
读取文件代码:
File file = new File(sourceFilePath);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
StringBuilder strBuilder = new StringBuilder();
String sLine = null;
while ((sLine = br.readLine()) != null) {
strBuilder.append(sLine);
strBuilder.append("\r\n");
}
解决方案
当我的应用程序在文件上等待 kind == ENTRY_MODIFY 时,我看到了类似的问题,但没有像您所拥有的 3 分钟延迟那样。您的代码不完整,因此不清楚是否缺少解释该滞后的重要位,因此很高兴看到完整的代码。
不幸的是,观察者服务可以为每个文件生成大量事件,尤其是 ENTRY_MODIFY。我发现在循环中调用 watcher.poll(timeout) 并整理一组接收到的事件更可靠。如果 set 变大,或者 watcher.poll 返回 null 或自上次处理以来的某个时间间隔,则仅执行一次。就像是:
var modified = new HashSet<Path>();
while(runningFlag) {
WatchKey k = ws.poll(modified.size() == 0 ? 10 : 1, TimeUnit.SECONDS);
if (k != null) {
// Loop through the ENTRY_MODIFY events as before but save the items:
... modified.add(fileName);
if (!k.reset()) { ... handle reconnection }
}
if (modified.size() > 0 && (k == null || modified.size() > limit)) {
// Act on modifications set
...
modified.clear();
}
}
这将减少您操作的事件数量,尽管您可能还有其他一些网络安装/缓存问题需要在这里处理。
推荐阅读
- react-native - React Native 和 react-native-ble-plx
- android - 以编程方式设置属性格式颜色android
- google-docs - 从 Google Doc 中获取值
- plot - 绘制最小二乘法
- arduino - 在Arduino中通过串行控制伺服
- amazon-web-services - 如果架构保持不变,是否需要更新分区,但添加了新数据
- python - 列表中值的 Python 索引
- javascript - 改变 antd 下拉菜单的位置(自动完成)
- regex - 正则表达式非贪婪匹配贪婪地使用空格
- ruby-on-rails - 带有 React Native 项目的 Rails 服务器出现错误:未处理的承诺拒绝:TypeError:对象不是函数