2021-02-03 遇到的一点小问题
最近有个需求需要监听文件夹里文件的变化,网上找到 如何用 Shell 监控文件变化? ,安装 pacman -Sy inotify-tools ,参照 文档 试了下 inotifywait ,能实现挺多功能的。
java 的WatchService 类提供了一种方式可以检查
try
{
WatchService watchService = FileSystems.getDefault()
.newWatchService()
Path path = Paths.get(pathName)
// 注册监听器
path.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE)
while (true)
{
// 阻塞方式,消费文件更改事件
List<WatchEvent<?>>watchEvents = watchService.take()
.pollEvents()
for (WatchEvent<?>watchEvent : watchEvents)
{
System.out.printf("[%s]文件发生了[%s]事件。%n", watchEvent
.context(), watchEvent.kind())
}
}
}
catch (Exception e)
{
}
inotify can not monitor nfs or sambatwo way to do it :
1>File Alteration Monitor(known as FAM and sgi_fam, provides a subsystem developed by Silicon Graphics for Unix-likeoperating systems.)
server runs at nfs server ,while client runs at client pc
2>server runs a program using inotif monitor file changs ,then send changs to remote client by socket. it is a question of adding some socket ipc to your client progarm.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)