Java中的目录监听器
我有一个应用程序,我想要听取对特定目录所做的任何更改。 只要在该目录中添加,删除或更新了任何文件,应用程序就应该能够ping通。
您可以使用JNotify
JNotify是一个Java库,允许Java应用程序监听文件系统事件,如:文件创build文件修改文件重命名文件删除支持的平台
Windows(2000或更新版本)Windows笔记支持INofity的Linux(2.6.14或更新版本)Linux笔记Mac OS X(10.5或更新版本)Mac OS笔记
更多信息 :
从这里下载JNotify
解压zip,把.dll / .so根据你的libpath中的平台。 并创build一个类,在类path中提供jnotify-0.93.jar
。
示例代码:
package org.life.java.stackoverflow.questions; import net.contentobjects.jnotify.JNotify; import net.contentobjects.jnotify.JNotifyListener; /** * * @author Jigar */ public class JNotifyDemo { public void sample() throws Exception { // path to watch String path = System.getProperty("user.home"); // watch mask, specify events you care about, // or JNotify.FILE_ANY for all events. int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED; // watch subtree? boolean watchSubtree = true; // add actual watch int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener()); // sleep a little, the application will exit if you // don't (watching is asynchronous), depending on your // application, this may not be required Thread.sleep(1000000); // to remove watch the watch boolean res = JNotify.removeWatch(watchID); if (!res) { // invalid watch ID specified. } } class Listener implements JNotifyListener { public void fileRenamed(int wd, String rootPath, String oldName, String newName) { print("renamed " + rootPath + " : " + oldName + " -> " + newName); } public void fileModified(int wd, String rootPath, String name) { print("modified " + rootPath + " : " + name); } public void fileDeleted(int wd, String rootPath, String name) { print("deleted " + rootPath + " : " + name); } public void fileCreated(int wd, String rootPath, String name) { print("created " + rootPath + " : " + name); } void print(String msg) { System.err.println(msg); } } public static void main(String[] args) throws Exception { new JNotifyDemo().sample(); } }
输出:
modified C:\Documents and Settings\jigar: LOCALS~1\Temp\etilqs_4s8ywsvyukghK0uDxRop modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_4s8ywsvyukghK0uDxRop modified C:\Documents and Settings\jigar : LOCALS~1\Temp\output1295531079119 modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default deleted C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001ea9 created C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae modified C:\Documents and Settings\jigar : LOCALS~1\Temp\output1295531079119 modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Current Session deleted C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001ea8 created C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf
从Java 1.7开始,您可以使用Watch Service API来注册目录事件。 它是Java的新I / O (NIO)库的一部分,不需要任何额外的资源。 在官方文档中可以find如何使用API的示例。
注册WatchService后,您可以像这样获取目标path的事件:
for (WatchEvent<?> event: key.pollEvents()) { // Context for directory entry event is the file name of entry WatchEvent<Path> ev = cast(event); Path name = ev.context(); Path child = dir.resolve(name); // print out event System.out.format("%s: %s\n", event.kind().name(), child); }
在Java中通知文件通知。 代码示例
public void sample() throws Exception { // path to watch String path = System.getProperty("user.home"); // watch mask, specify events you care about, // or JNotify.FILE_ANY for all events. int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED; // watch subtree? boolean watchSubtree = true; // add actual watch int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener()); // sleep a little, the application will exit if you // don't (watching is asynchronous), depending on your // application, this may not be required Thread.sleep(1000000); // to remove watch the watch boolean res = JNotify.removeWatch(watchID); if (!res) { // invalid watch ID specified. } } class Listener implements JNotifyListener { public void fileRenamed(int wd, String rootPath, String oldName, String newName) { print("renamed " + rootPath + " : " + oldName + " -> " + newName); } public void fileModified(int wd, String rootPath, String name) { print("modified " + rootPath + " : " + name); } public void fileDeleted(int wd, String rootPath, String name) { print("deleted " + rootPath + " : " + name); } public void fileCreated(int wd, String rootPath, String name) { print("created " + rootPath + " : " + name); } void print(String msg) { System.err.println(msg); } }