您可以使用JNotify
JNotify是一个Java库,允许Java应用程序侦听文件系统事件,例如:创建文件已修改文件已重命名文件已删除文件支持的平台
Windows(2000或更高版本)Windows注释支持INofity的Linux(2.6.14或更高版本)Linux注释Mac OS
X(10.5或更高版本)Mac OS注释
更多信息 :
从这里下载JNotify
解压缩该zip文件,然后根据平台将.dll / .so放入您的lib路径中。并
jnotify-0.93.jar在类路径中创建一个类提供。
样例代码:
package org.life.java.stackoverflow.questions;import net.contentobjects.jnotify.JNotify;import net.contentobjects.jnotify.JNotifyListener;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 Settingsjigar: LOCALS~1Tempetilqs_4s8ywsvyukghK0uDxRopmodified C:documents and Settingsjigar : LOCALS~1Tempetilqs_4s8ywsvyukghK0uDxRopmodified C:documents and Settingsjigar : LOCALS~1Tempoutput1295531079119modified C:documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultdeleted C:documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001ea9created C:documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001eaemodified C:documents and Settingsjigar : LOCALS~1Tempetilqs_04gchL79ZJrpClZIqiommodified C:documents and Settingsjigar : LOCALS~1Tempetilqs_04gchL79ZJrpClZIqiommodified C:documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachemodified C:documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001eaemodified C:documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001eaemodified C:documents and Settingsjigar : LOCALS~1Tempoutput1295531079119modified C:documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCurrent Sessiondeleted C:documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001ea8created C:documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001eafmodified C:documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachemodified C:documents and Settingsjigar : LOCALS~1Tempetilqs_04gchL79ZJrpClZIqiommodified C:documents and Settingsjigar : LOCALS~1Tempetilqs_04gchL79ZJrpClZIqiommodified C:documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001eafmodified C:documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001eaf



