java监控文件更新_Java监控文件变化

NIO.2的Path类提供了如下的一个方法来监听文件系统的变化。

register(WatcherService watcher,WatchEvent.Kind>... events):用watcher监听该path代表的目录下文件变化。event参数指定要监听哪些类型的事件。

WatchService有三个方法来监听目录的文件变化事件。

WatchKey poll():获取下一个WatchKey,如果没有WatchKey发生就立即返回null;

WatcheKey poll(long timeout,TimeUnit unit):尝试等待timeout时间去获取下一个WatchKey;

WatchKey  take():获取下一个WatchKey,如果没有发生就一直等待;

如果程序需要一直监控,则应该选择使用take()方法,如果程序只需要监控指定时间,则使用poll方法。

import java.nio.file.FileSystems;

import java.nio.file.Paths;

import java.nio.file.StandardWatchEventKinds;

import java.nio.file.WatchEvent;

import java.nio.file.WatchKey;

import java.nio.file.WatchService;

public class Test {

public static void main(String[] args) throws Exception

{

WatchService watchService=FileSystems.getDefault().newWatchService();

Paths.get("C:/").register(watchService,

StandardWatchEventKinds.ENTRY_CREATE,

StandardWatchEventKinds.ENTRY_DELETE,

StandardWatchEventKinds.ENTRY_MODIFY);

while(true)

{

WatchKey key=watchService.take();

for(WatchEvent> event:key.pollEvents())

{

System.out.println(event.context()+"发生了"+event.kind()+"事件");

}

if(!key.reset())

{

break;

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值