码迷,mamicode.com
首页 > 其他好文 > 详细

JNotify的监测文件变化的简单测试例子

时间:2018-04-27 19:52:45      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:count   cts   inter   about   oid   async   style   throw   list   

一、理由

使用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的监测文件变化的简单测试例子

标签:count   cts   inter   about   oid   async   style   throw   list   

原文地址:https://www.cnblogs.com/songxingzhu/p/8963591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!