Java中实时监控目录下文件变化的两种实现方法介绍

Java中实时监控目录下文件变化的两种实现方法,一中是基于jdk7之后,java自带的文件监控工具(Watch Service),一种是使用Commons-io的monitor下的相关类可以处理对文件进行监控。

1. 基于WatchService实现文件监控

Java 7的新IO - NIO.2提供了一组新的类和方法,主要存在于java.nio包内。它完全取代了java.io.File与文件系统的交互,并提供了新的异步处理类,无需手动配置线程池和其他底层并发控制,便可在后台线程中执行文件和网络IO操作。

Watch Service API可用于将指定目录注册到监视服务上。注册时须指定事件类型,如文件创建,修改,删除等。相关类图如下:


WatchService是监视服务接口,在不同系统上有不同的实现类。实现了Watchable接口的对象方可注册监视服务,java.nio.file.Path实现了此接口。WatchKey表示Watchable对象和WatchService的关联关系,在注册时被创建。注册完成后,WatchKey将被置为'ready'状态,直到下列三种情况之一发生:

  1. WatchKey.cancel()被调用
  2. 被监控的目录不存在或不可访问
  3. WatchService对象被关闭

当文件改动发生时,WatchKey的状态将会被置为"signaled"然后被放入待处理队列中。WatchService提供了三种从队列中获取WatchKeys的方式:

  1. poll - 返回队列中的一个key。如果没有可用的key,将立即返回null。

  2. poll(long, TimeUnit) - 如果队列中存在可用的key则将之返回,否则在参数预置的时间内等待可用的key。TimeUnit用来指定前一个参数表示的时间是纳秒、毫秒或是其他的时间单位。
    例子:final WatchKey watchKey = watchService.poll(1, TimeUnit.MINUTES);将会等待1分钟

  3. take - 方法将会等待直到可用的key被返回。
    示例如下:


/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java监控SFTP服务器上的文件变化,可以使用JSch库。JSch是一个纯Java实现的SSH2协议的库,它提供了连接SSH服务器、进行文件传输、执行远程命令等功能。 下面是一个使用JSch监控SFTP服务器上的文件变化的示例代码: ```java import com.jcraft.jsch.*; public class SftpFileMonitor { public static void main(String[] args) { String host = "example.com"; // SFTP服务器地址 int port = 22; // SFTP服务器端口号 String username = "username"; // SFTP用户名 String password = "password"; // SFTP密码 String remoteFilePath = "/path/to/file"; // SFTP服务器上的文件路径 JSch jsch = new JSch(); try { // 连接SFTP服务器 Session session = jsch.getSession(username, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); // 打开SFTP通道 ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp"); sftpChannel.connect(); // 监控文件变化 SftpMonitor monitor = new SftpMonitor(remoteFilePath); sftpChannel.stat(remoteFilePath); // 确保文件存在 sftpChannel.addChannelSftpListener(monitor); monitor.start(); // 等待文件变化 while (true) { Thread.sleep(1000); } } catch (JSchException | InterruptedException | SftpException e) { e.printStackTrace(); } } private static class SftpMonitor extends ChannelSftpAdapter implements Runnable { private String remoteFilePath; private Thread thread; private boolean stopped; public SftpMonitor(String remoteFilePath) { this.remoteFilePath = remoteFilePath; } public void start() { thread = new Thread(this); thread.start(); } public void stop() { stopped = true; } @Override public void run() { while (!stopped) { try { Thread.sleep(5000); // 每5秒检查一次文件变化 SftpATTRS attrs = this.getStat(remoteFilePath); long mtime = attrs.getMTime(); if (mtime != this.lastModified) { this.lastModified = mtime; System.out.println("File changed: " + remoteFilePath); } } catch (Exception e) { e.printStackTrace(); } } } private long lastModified = -1; @Override public void handle(ChannelSftp channel, int type, String message) { if (type == ChannelSftp.SSH_FX_EOF) { stop(); } } } } ``` 上述代码,我们通过JSch连接SFTP服务器,并打开SFTP通道。然后,我们创建了一个SftpMonitor类来监控文件变化,它实现了ChannelSftpListener接口,可以监听文件上传、下载、删除等事件。SftpMonitor的run()方法会定时检查文件变化,如果文件的修改时间发生变化,则表示文件发生了变化。我们可以在控制台上输出文件变化的信息。 需要注意的是,SFTP服务器上的文件路径可以是相对路径或绝对路径,但必须是服务器上的路径。如果是相对路径,它是相对于当前用户的home目录。如果是绝对路径,它是相对于服务器的根目录

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值