首页 > 解决方案 > 当文件改变时,我读取文件,文件的内容总是一样的?

问题描述

我使用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");
}

标签: javafilebufferedreaderinputstreamreader

解决方案


当我的应用程序在文件上等待 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();
    }
}

这将减少您操作的事件数量,尽管您可能还有其他一些网络安装/缓存问题需要在这里处理。


推荐阅读