您的位置:首页 > 其它

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

 



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: