JNotify的监测文件变化的简单测试例子
Posted 宋兴柱-Sindrol
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JNotify的监测文件变化的简单测试例子相关的知识,希望对你有一定的参考价值。
一、理由
使用JNotify监测的更全面,更快速。
二、参考代码
import net.contentobjects.jnotify.JNotify; import net.contentobjects.jnotify.JNotifyException; import net.contentobjects.jnotify.JNotifyListener; public class Main implements JNotifyListener { public static void main(String[] args) throws JNotifyException, InterruptedException { // path to watch String path = "/weatherdata"; System.out.println(System.getProperty("java.library.path")); // 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 Main()); // 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); } int count = 0; @Override public void fileCreated(int i, String s, String s1) { System.out.println("fileCreated s=" + i + " i=" + s + " s1=" + s1); } @Override public void fileDeleted(int i, String s, String s1) { System.out.println("fileDeleted s=" + s + " i=" + i + " s1=" + s1); } @Override public void fileModified(int i, String s, String s1) { System.out.println("fileModified s=" + s + " i=" + i + " s1=" + s1); } @Override public void fileRenamed(int i, String s, String s1, String s2) { System.out.println("fileRenamed s=" + s + " i=" + i + " s1=" + s1 + " s2=" + s2 + " count:" + (++count)); } }
三、说明
如果启动时报找不到JNotify库(https://sourceforge.net/projects/jnotify/files/jnotify/jnotify-0.94/ 下载),请复制JNotify的最新版本库到系统相应库目录下。
System.getProperty("java.library.path")
在Windows上,复制jnotify_64bit.dll到:c:/windows下即可。
在Linux上,可以复制libjnotify.so到/usr/lib64目录下。
附件:https://files.cnblogs.com/files/songxingzhu/jnotify-lib-0.94.zip
以上是关于JNotify的监测文件变化的简单测试例子的主要内容,如果未能解决你的问题,请参考以下文章