是否有针对 Java 的复杂文件系统监视器,它是免费软件还是开源软件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1096404/
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-10-29 15:09:49  来源:igfitidea点击:

Is there a sophisticated file system monitor for Java which is freeware or open source?

javafile-monitoring

提问by Daniel Rikowski

I'm looking for a Java library which can notify me about changes on the file system.

我正在寻找一个 Java 库,它可以通知我有关文件系统更改的信息。

I found some free libraries, but they all use brute-force detection, i.e. polling in regular intervals. And the other ones are always commercial.

我找到了一些免费的库,但它们都使用蛮力检测,即定期轮询。而其他的总是商业化的。

What I'm looking for is a library which uses the file monitor functions from the OS, i.e. FindFirstChangeNotificationfor Win32, FAMfor Linux and whatever is available on other OS. I don't mind if brute-force is used as fallback, but for Windows and Linux it should use the available OS features.

我正在寻找的是一个库,它使用操作系统中的文件监视器功能,即 Win32 的FindFirstChangeNotification、Linux 的FAM以及其他操作系统上可用的任何功能。我不介意是否使用蛮力作为后备,但对于 Windows 和 Linux,它应该使用可用的操作系统功能。

回答by Brian Agnew

JNotifyseems to do what you require.

JNotify似乎可以满足您的要求。

Polling a filesystem (say, a directory for update time changes) won't impose a significant load on your system, and shouldn't be discounted. Apps like Tomcat etc. use this for managing hot deploys with few problems.

轮询文件系统(例如,更新时间更改的目录)不会对您的系统造成很大的负载,并且不应打折扣。Tomcat 等应用程序使用它来管理热部署,几乎没有问题。

It's not a lot of help now, but the upcoming Java 7 has a WatchServiceprecisely for this.

现在帮助不大,但即将到来的 Java 7 有一个WatchService正是为此。

回答by Stu Thompson

If you are comfortable working on the bleeding edge of Java, have a look at JSR 203: More New I/O APIsaka nio2 which is due out in Java 7.

如果您对 Java 的前沿工作感到满意,请查看JSR 203:更多新的 I/O API,又名 nio2,该API将在Java 7 中推出

A new filesystem interface that supports bulk access to file attributes, change notification, escape to filesystem-specific APIs, and a service-provider interface for pluggable filesystem implementations;

一个新的文件系统接口,支持对文件属性的批量访问、更改通知、转义到文件系统特定的 API,以及一个用于可插拔文件系统实现的服务提供者接口;

回答by mumhero

jpathwatchis another option. It implements Java 7′s API for directory monitoring, so it is easy to port to Java 7 if desired.

jpathwatch是另一种选择。它实现了 Java 7 的用于目录监控的 API,因此如果需要,可以很容易地移植到 Java 7。

回答by hhafez

Why does it have to be Java?

为什么必须是Java?

The Library that is going to use platform specific API must have native code, so essentially the library is going to be wrappers around the native code that makes the usage of the different APIs transparent.

将使用平台特定 API 的库必须具有本机代码,因此基本上该库将成为本机代码的包装器,使不同 API 的使用变得透明。

I suggest if you do not find the Library your self then you will have to either use brute force as you call it, or call the native API using JNI for each platform.

我建议如果你没有找到你自己的库,那么你将不得不在调用它时使用蛮力,或者为每个平台使用 JNI 调用本机 API。

回答by Raman

Two more options that currentlyuse polling:

目前使用轮询的另外两个选项:

However, because both projects have large active communities, they will likely be updated in the future to support the Java 7 WatchService (FileSystem.newWatchService()), which doesuse the underlying platform's notification facilities, if any.

但是,因为这两个项目都有大量活跃的社区,所以它们将来可能会更新以支持 Java 7 WatchService ( FileSystem.newWatchService()),它确实使用了底层平台的通知工具(如果有的话)。

Both libraries also provide a useful abstraction layer for integrating with various other transports and systems, so that your main application code is not dependent on the inputs being received from a filesystem.

这两个库还提供了一个有用的抽象层,用于与各种其他传输和系统集成,以便您的主要应用程序代码不依赖于从文件系统接收的输入。