栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java中的目录侦听器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中的目录侦听器

您可以使用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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/486358.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号