简体   繁体   English

Java:监视目录中的更改

[英]Java: Watching a Directory for Changes

I want to create a listener to detect directory change recursively (the main directory and its subdirectories). 我想创建一个侦听器以递归检测目录更改(主目录及其子目录)。 After a web search I found this link that explains the use of the class WatchService from the Package java.nio.file that api exactly meets my needs but unfortunately it is only available in Java 7! 经过网络搜索后,我发现此链接说明了如何使用api完全满足我的需求的Package java.nio.file中的WatchService类的用法,但不幸的是,它仅在Java 7中可用! Then I return to search again in order to find a framework that allows the same and is compatible java 5 and java 6 but again there was a problem, because recursion is available for Windows and my application should use Linux!! 然后我再次返回搜索以找到一个允许相同并且兼容Java 5和Java 6的框架 ,但是又出现了一个问题,因为递归可用于Windows,我的应用程序应该使用Linux! Can you offer me a solution: another framework, a way to do.. 您能给我一个解决方案吗:另一种框架,一种执行方法。

I think you did a good discovery job and found a wonderful library jpathwatch. 我认为您做得很好,发现了一个很棒的库jpathwatch。 I do not understand what was your problem with recursion: I have not seen any restriction for linux in this library documentation. 我不明白您的递归问题是什么:在此库文档中,我还没有看到对linux的任何限制。

But if for some reason jpathwatch cannot help you on linux, it is not a problem: you can run du command yourself. 但是,如果出于某种原因jpathwatch无法在Linux上为您提供帮助,那不是问题:您可以自己运行du命令。 See this reference: http://linux.about.com/library/cmd/blcmdl1_du.htm 请参阅此参考: http : //linux.about.com/library/cmd/blcmdl1_du.htm

If I were you I probably do the following: write simple script that runs du in loop. 如果您是我,我可能会执行以下操作:编写循环运行du的简单脚本。 Then run this script from java from separate thread that is contiguously reading the script's output and analyses it. 然后从连续读取脚本输出并对其进行分析的单独线程中,从Java运行此脚本。

This is a kind of functionality that requires support of JVM or a native library, such as the one you've found for Windows. 这是一种功能,需要支持JVM或本机库,例如您在Windows中找到的库。 If you can't find anything in Java for Linux, I suggest asking for a binary Linux library (in a different question) and then build a Java native class on top of that. 如果在Java for Linux中找不到任何内容,建议您使用二进制Linux库(在另一个问题中),然后在此之上构建Java本机类。

I hope other people will help you better. 希望其他人能对您有所帮助。

To do this on Linux you need to use Java 7, or a native library that uses inotify . 要在Linux上执行此操作,您需要使用Java 7或使用inotify的本机库。 Have you considered the JNotify library ? 您是否考虑过JNotify库 It looks like it handles recursion into subdirectories, including newly created ones. 它看起来像处理递归到子目录,包括新创建的子目录。

Have a look at http://download.oracle.com/javase/tutorial/essential/io/notification.html 看看http://download.oracle.com/javase/tutorial/essential/io/notification.html

"The java.nio.file package provides a file change notification API, called the Watch Service API. This API enables you to register a directory (or directories) with the watch service. When registering, you tell the service which types of events you are interested in: file creation, file deletion, or file modification. When the service detects an event of interest, it is forwarded to the registered process. The registered process has a thread (or a pool of threads) dedicated to watching for any events it has registered for. When an event comes in, it is handled as needed." “ java.nio.file包提供了一个名为Watch Service API的文件更改通知API。该API使您可以向watch服务注册一个目录(或多个目录)。注册时,您可以告诉服务您事件的类型对以下内容感兴趣:文件创建,文件删除或文件修改。当服务检测到感兴趣的事件时,它将转发到已注册的进程。已注册的进程具有专用于监视任何事件的线程(或线程池)它已经注册。当事件发生时,将根据需要进行处理。”

Update: Ooops, just realized you already saw this. 更新:糟糕,您刚刚意识到您已经看到了这一点。 I didn't realize this was only in Java 7 :-( 我没有意识到这仅在Java 7中:-(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM