java本身不能直接监听系统的文件操作事件,但可以用第三方开源软件监控。
在这里介绍
JNotify 大家可以在sourceforge上去下载。说说用法,其实真的好简单代码如下
1.下载成功后,把jnotify.dll放到system32下面,不然就会报错Exception in thread "main" java.lang.UnsatisfiedLinkError: no jnotify in java.library.path
2.写一个类implements JNotifyListener,
public class JnotifyListener implements JNotifyListener {
@Override
public void fileCreated(int arg0, String arg1, String arg2) {
System.out.println("fileCreate path:" + arg1);
System.out.println("fileCreate name:" + arg2);
}
@Override
public void fileDeleted(int arg0, String arg1, String arg2) {
System.out.println("fileDeleted path:" + arg1);
System.out.println("fileDeleted name :" + arg2);
}
@Override
public void fileModified(int arg0, String arg1, String arg2) {
System.out.println("fileModified path:" + arg1);
System.out.println("fileModified name:" + arg2);
}
@Override
public void fileRenamed(int arg0, String arg1, String arg2, String arg3) {
System.out.println("fileRenamed path:" + arg1);
System.out.println("fileRenamedname:" + arg2);
}
}
然后写个测试类
public class TestJnotify {
public static void main(String[] args) throws JNotifyException {
int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED |JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
JNotify.addWatch("e:/testListener", mask, true, new JnotifyListener());
for (;;) {
}
}
}
这样就可以实现了
posted on 2011-06-24 17:29
青菜猫(孙宇) 阅读(2153)
评论(1) 编辑 收藏 所属分类:
java