Java中的文件更改侦听器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/494869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:20:58  来源:igfitidea点击:

File changed listener in Java

javafilelistener

提问by cheng81

I'd like to be notified when a file has been changed in the file system. I have found nothing but a thread that polls the lastModified File property and clearly this solution is not optimal.

我希望在文件系统中的文件发生更改时收到通知。我只找到了一个轮询 lastModified File 属性的线程,显然这个解决方案不是最佳的。

采纳答案by Rutesh Makhijani

At the low level the only way to model this utility is to have a thread polling on a directory and keeping a watch on the attributes of the file. But you can use patterns to develop a adapter for such a utility.

在底层,对该实用程序建模的唯一方法是让线程轮询目录并监视文件的属性。但是您可以使用模式为此类实用程序开发适配器。

For example j2ee application servers like Tomcat and others have a auto load feature where in as soon as deployment descriptor changes or servlet class changes the application restarts.

例如,像 Tomcat 和其他人这样的 j2ee 应用程序服务器具有自动加载功能,一旦部署描述符更改或 servlet 类更改,应用程序就会重新启动。

You can use the libraries from such servers as most of the code of tomcat is reusable and opensource.

您可以使用来自此类服务器的库,因为 tomcat 的大部分代码都是可重用且开源的。

回答by jdh80

If you are willing to part with some money, JNIWrapper is a useful library with a Winpack, you will be able to get file system events on certain files. Unfortunately windows only.

如果您愿意花一些钱,JNIWrapper 是一个带有 Winpack 的有用库,您将能够获取某些文件的文件系统事件。不幸的是只有窗户。

See https://www.teamdev.com/jniwrapper.

请参阅https://www.teamdev.com/jniwrapper

Otherwise, resorting to native code is not always a bad thing especially when the best on offer is a polling mechanism as against a native event.

否则,诉诸本机代码并不总是一件坏事,尤其是当提供的最好的方法是针对本机事件的轮询机制时。

I've noticed that Java file system operations can be slow on some computers and can easily affect the application's performance if not handled well.

我注意到 Java 文件系统操作在某些计算机上可能会很慢,如果处理不当,很容易影响应用程序的性能。

回答by André

There is a lib called jnotifythat wraps inotifyon linux and has also support for windows. Never used it and I don't know how good it is, but it's worth a try I'd say.

有一个名为jnotify的库,它在 linux上包装了inotify,并且还支持 windows。从来没有用过它,我不知道它有多好,但我想说值得一试。

回答by Stephen Denne

I've written a log file monitor before, and I found that the impact on system performance of polling the attributes of a single file, a few times a second, is actually very small.

之前写过一个日志文件监视器,我发现轮询单个文件的属性,每秒几次,对系统性能的影响其实很小。

Java 7, as part of NIO.2 has added the WatchService API

Java 7,作为 NIO.2 的一部分添加了WatchService API

The WatchService API is designed for applications that need to be notified about file change events.

WatchService API 设计用于需要通知文件更改事件的应用程序。

回答by Tom Hawtin - tackline

"More NIO features" has file watch functionality, with implementation dependent upon the underlying OS. Should be in JDK7.

“更多 NIO 功能”具有文件监视功能,其实现取决于底层操作系统。应该在JDK7中。

回答by Telcontar

I use the VFS API from Apache Commons, here is an example of how to monitor a file without much impact in performance:

我使用来自 Apache Commons 的 VFS API,这里有一个示例,说明如何在不影响性能的情况下监控文件:

DefaultFileMonitor

默认文件监视器

回答by Telcontar

There is a commercial cross-desktop library for files and folders watching called JxFileWatcher. It can be downloaded from here: http://www.teamdev.com/jxfilewatcher/

有一个用于文件和文件夹监视的商业跨桌面库,称为 JxFileWatcher。可以从这里下载:http: //www.teamdev.com/jxfilewatcher/

Also you can see it in action online: http://www.teamdev.com/jxfilewatcher/onlinedemo/

你也可以在网上看到它:http: //www.teamdev.com/jxfilewatcher/onlinedemo/

回答by onejigtwojig

You may also consider the Apache Commons JCI (Java Compiler Interface). Although this API seems to be focused on dynamic compilation of classes, it also includes classes in its API that monitors file changes.

您还可以考虑 Apache Commons JCI(Java 编译器接口)。虽然这个 API 似乎专注于类的动态编译,但它的 API 中也包含了监控文件更改的类。

Example: http://commons.apache.org/jci/usage.html

示例:http: //commons.apache.org/jci/usage.html

回答by Upgradingdave

Spring Integration provides a nice mechanism for watching Directories and files: http://static.springsource.org/spring-integration/reference/htmlsingle/#files. Pretty sure it's cross platform (I've used it on mac, linux, and windows).

Spring Integration 提供了一个很好的机制来观察目录和文件:http: //static.springsource.org/spring-integration/reference/htmlsingle/#files。很确定它是跨平台的(我已经在 mac、linux 和 windows 上使用过它)。

回答by Mike Kramer

I run this snippet of code every time I go to read the properties file, only actually reading the file if it has been modified since the last time I read it. Hope this helps someone.

每次我去读取属性文件时,我都会运行这段代码,只有在自上次读取文件以来它被修改过的情况下才真正读取该文件。希望这可以帮助某人。

private long timeStamp;
private File file;

private boolean isFileUpdated( File file ) {
  this.file = file;
  this.timeStamp = file.lastModified();

  if( this.timeStamp != timeStamp ) {
    this.timeStamp = timeStamp;
    //Yes, file is updated
    return true;
  }
  //No, file is not updated
  return false;
}

Similar approach is used in Log4J FileWatchdog.

Log4J 中使用了类似的方法FileWatchdog