java实时监控文件行尾内容的实现

 更新时间:2020年02月10日 09:23:09   作者:油头粉面  
这篇文章主要介绍了java实时监控文件行尾内容的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

今天讲一下怎样用Java实现实时的监控文件行尾的追加内容,类似Linux命令

tail -f

在之前的面试中遇到过一个问题,就是用Java实现tail功能,之前的做法是做一个定时任务每隔1秒去读取一次文件,去判断内容是否有追加,如果有则输出新追加的内容,这个做法虽然能勉强实现功能,但是有点太low,今天采用另外一种实现方式,基于事件通知。

1.WatchService

首先介绍一下WatchService类,WatchService可以监控某一个目录下的文件的变动(新增,修改,删除)并以事件的形式通知文件的变更,这里我们可以实时的获取到文件的修改事件,然后计算出追加的内容,Talk is cheap,Show me the code.

Listener

简单的接口,只有一个fire方法,当事件发生时处理事件。

  public interface Listener {

  /**
   * 发生文件变动事件时的处理逻辑
   * 
   * @param event
   */
  void fire(FileChangeEvent event);
}

FileChangeListener

Listener接口的实现类,处理文件变更事件。

public class FileChangeListener implements Listener {

  /**
   * 保存路径跟文件包装类的映射
   */
  private final Map<String, FileWrapper> map = new ConcurrentHashMap<>();

  public void fire(FileChangeEvent event) {
    switch (event.getKind().name()) {
    case "ENTRY_MODIFY":
      // 文件修改事件
      modify(event.getPath());
      break;
    default:
      throw new UnsupportedOperationException(
          String.format("The kind [%s] is unsupport.", event.getKind().name()));
    }
  }

  private void modify(Path path) {
    // 根据全路径获取包装类对象
    FileWrapper wrapper = map.get(path.toString());
    if (wrapper == null) {
      wrapper = new FileWrapper(path.toFile());
      map.put(path.toString(), wrapper);
    }
    try {
      // 读取追加的内容
      new ContentReader(wrapper).read();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

}

FileWrapper

文件包装类,包含文件和当前读取的行号

public class FileWrapper {

  /**
   * 当前文件读取的行数
   */
  private int currentLine;

  /**
   * 监听的文件
   */
  private final File file;

  public FileWrapper(File file) {
    this(file, 0);
  }

  public FileWrapper(File file, int currentLine) {
    this.file = file;
    this.currentLine = currentLine;
  }

  public int getCurrentLine() {
    return currentLine;
  }

  public void setCurrentLine(int currentLine) {
    this.currentLine = currentLine;
  }

  public File getFile() {
    return file;
  }

}

FileChangeEvent

文件变更事件

public class FileChangeEvent {

  /**
   * 文件全路径
   */
  private final Path path;
  /**
   * 事件类型
   */
  private final WatchEvent.Kind<?> kind;

  public FileChangeEvent(Path path, Kind<?> kind) {
    this.path = path;
    this.kind = kind;
  }

  public Path getPath() {
    return this.path;
  }

  public WatchEvent.Kind<?> getKind() {
    return this.kind;
  }

}

ContentReader

内容读取类

public class ContentReader {

  private final FileWrapper wrapper;

  public ContentReader(FileWrapper wrapper) {
    this.wrapper = wrapper;
  }

  public void read() throws FileNotFoundException, IOException {
    try (LineNumberReader lineReader = new LineNumberReader(new FileReader(wrapper.getFile()))) {
      List<String> contents = lineReader.lines().collect(Collectors.toList());
      if (contents.size() > wrapper.getCurrentLine()) {
        for (int i = wrapper.getCurrentLine(); i < contents.size(); i++) {
          // 这里只是简单打印出新加的内容到控制台
          System.out.println(contents.get(i));
        }
      }
      // 保存当前读取到的行数
      wrapper.setCurrentLine(contents.size());
    }
  }

}

DirectoryTargetMonitor

目录监视器,监控目录下文件的变化

public class DirectoryTargetMonitor {

  private WatchService watchService;

  private final FileChangeListener listener;

  private final Path path;

  private volatile boolean start = false;

  public DirectoryTargetMonitor(final FileChangeListener listener, final String targetPath) {
    this(listener, targetPath, "");
  }

  public DirectoryTargetMonitor(final FileChangeListener listener, final String targetPath, final String... morePaths) {
    this.listener = listener;
    this.path = Paths.get(targetPath, morePaths);
  }

  public void startMonitor() throws IOException {
    this.watchService = FileSystems.getDefault().newWatchService();
    // 注册变更事件到WatchService
    this.path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
    this.start = true;
    while (start) {
      WatchKey watchKey = null;
      try {
        // 阻塞直到有事件发生
        watchKey = watchService.take();
        watchKey.pollEvents().forEach(event -> {
          WatchEvent.Kind<?> kind = event.kind();
          Path path = (Path) event.context();
          Path child = this.path.resolve(path);
          listener.fire(new FileChangeEvent(child, kind));
        });
      } catch (Exception e) {
        this.start = false;
      } finally {
        if (watchKey != null) {
          watchKey.reset();
        }
      }
    }
  }

  public void stopMonitor() throws IOException {
    System.out.printf("The directory [%s] monitor will be stop ...\n", path);
    Thread.currentThread().interrupt();
    this.start = false;
    this.watchService.close();
    System.out.printf("The directory [%s] monitor will be stop done.\n", path);
  }

}

测试类

在D盘新建一个monitor文件夹, 新建一个test.txt文件,然后启动程序,程序启动完成后,我们尝试往test.txt添加内容然后保存,控制台会实时的输出我们追加的内容,PS:追加的内容要以新起一行的形式追加,如果只是在原来的尾行追加,本程序不会输出到控制台,有兴趣的同学可以扩展一下

  public static void main(String[] args) throws IOException {
    DirectoryTargetMonitor monitor = new DirectoryTargetMonitor(new FileChangeListener(), "D:\\monitor");
    monitor.startMonitor();
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • springboot 集成支付宝支付的示例代码

    springboot 集成支付宝支付的示例代码

    这篇文章主要介绍了springboot 集成支付宝支付的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • Spring Boot 简介(入门篇)

    Spring Boot 简介(入门篇)

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。下面通过本文给大家介绍spring boot相关知识,需要的的朋友参考下吧
    2017-04-04
  • Java学习笔记之面向对象编程精解

    Java学习笔记之面向对象编程精解

    看名字它是注重对象的。当解决一个问题的时候,面向对象会把事物抽象成对象的概念,就是说这个问题里面有哪些对象,然后给对象赋一些属性和方法,然后让每个对象去执行自己的方法,问题得到解决
    2021-09-09
  • 浅析java修饰符访问权限(动力节点Java学院整理)

    浅析java修饰符访问权限(动力节点Java学院整理)

    Java有四种访问权限,其中三种有访问权限修饰符,分别为private,public和protected,还有一种不带任何修饰符,下面通过本文给大家简单介绍下java修饰符访问权限相关知识,感兴趣的朋友一起学习吧
    2017-04-04
  • 解析Nacos的API居然存在这么严重的漏洞

    解析Nacos的API居然存在这么严重的漏洞

    这篇文章主要介绍了Nacos的API居然存在这么严重的漏洞,Nacos为我们提供了大量API,但是这些API默认是没有开启认证的,直接可以访问,针对于这一点我们也都可以去验证一下,本文给大家详细讲解,感兴趣的朋友跟随小编一起看看吧
    2022-09-09
  • SpringBoot整合HikariCP数据库连接池方式

    SpringBoot整合HikariCP数据库连接池方式

    这篇文章主要介绍了SpringBoot整合HikariCP数据库连接池方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • JAVA为什么要使用封装及如何封装经典实例

    JAVA为什么要使用封装及如何封装经典实例

    这篇文章主要给大家介绍了关于JAVA为什么要使用封装及如何封装的相关资料,封装就是将属性私有化,提供公有的方法访问私有属性,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-10-10
  • Java设计模式之建造者模式的示例详解

    Java设计模式之建造者模式的示例详解

    建造者模式,是一种对象构建模式 它可以将复杂对象的建造过程抽象出来,使这个抽象过程的不同实现方法可以构造出不同表现的对象。本文将通过示例讲解建造者模式,需要的可以参考一下
    2022-02-02
  • Spring学习通过AspectJ注解方式实现AOP操作

    Spring学习通过AspectJ注解方式实现AOP操作

    这篇文章主要为大家介绍了Spring学习通过AspectJ注解方式实现AOP操作,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • 详解spring中使用Elasticsearch的代码实现

    详解spring中使用Elasticsearch的代码实现

    本篇文章主要介绍了详解spring中使用Elasticsearch的代码实现,具有一定的参考价值,有兴趣的可以了解一下
    2017-05-05

最新评论