JNotify的监测文件变化的简单测试例子
2018-04-27 17:28
399 查看
一、理由
使用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
相关文章推荐
- 一个简单的上传文件测试例子
- SpringMvc(注解)上传文件的简单例子
- Jedis几个简单测试例子
- 一个简单的jQuery插件ajaxfileupload实现ajax上传文件例子
- 【IO面试题】文件操作,如何列出某个目录下的所有文件?如何列出某个目录下的所有子目录?写个简单的例子
- Makefile文件简单示例一:测试验证小程序的管理
- arm调用thumb(相同文件和不同文件)并且返回 简单小例子
- C++调用Lua读取配置文件的一个简单例子
- springmvc笔记之 -- 配置文件简单例子
- 5、Cocos2dx 3.0小游戏开发的例子寻找测试三个简单的介绍和总结
- 一个简单的jQuery插件ajaxfileupload.js实现ajax上传文件例子
- OOP编程iBatis 学习笔记之- 环境配置及简单例子测试
- Tomcat服务器简单测试jsp文件和html文件
- bochs配置文件最简单的例子
- 自定义注解的简单实现例子,附上代码和测试结果图
- c++ 读写复合文件简单例子
- struts简单例子的分析(含代码注释和配置文件说明)
- 使用jmeter进行接口压力测试的一个简单例子
- 递归调用简单例子——按层次列出文件夹下所有文件和文件夹
- c++程序中写测试log到文件的简单实现