JNotify

 http://jnotify.sourceforge.net/ 

JNotify通过JNI调用本地的方法.,(windows:xxx.dll,linux:xxx.so)

  1. 下载JNotify.

  2. 添加对应的文件到 System.getProperty("java.library.path")下.

  3. 添加jar

来自官网的文档:

package jnotify;

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;

/**
 *
 * @author D04
 */
public class Jnotify {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        //System.out.println("path-->" + System.getProperty("user.home"));
        System.out.println("java.library.path-->" + System.getProperty("java.library.path"));
        new Jnotify().sample();
    }

    public void sample() throws Exception {
        // path to watch
//        String path = System.getProperty("user.home");
        String path = "D:\\testJnotify";

    // 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 Listener());

    // 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);
        if (!res) {
            // invalid watch ID specified.
        }
    }

    class Listener implements JNotifyListener {

        @Override
        public void fileRenamed(int wd, String rootPath, String oldName,
                String newName) {
            print("renamed " + rootPath + " : " + oldName + " -> " + newName);
        }

        @Override
        public void fileModified(int wd, String rootPath, String name) {
            print("modified " + rootPath + " : " + name);
        }

        @Override
        public void fileDeleted(int wd, String rootPath, String name) {
            print("deleted " + rootPath + " : " + name);
        }

        public void fileCreated(int wd, String rootPath, String name) {
            print("created " + rootPath + " : " + name);
        }

        void print(String msg) {
            System.err.println(msg);
        }
    }

}


转载于:https://my.oschina.net/fuckmylife0/blog/324967

03-08
### JNotify Java 文件变更通知库使用方法 JNotify 是一款用于监控文件系统变化的 Java 库,能够监听多种类型的文件操作事件。通过该库的应用程序可以在指定路径下的文件发生创建、删除、修改或重命名时得到即时的通知。 为了集成并利用 JNotify 进行文件系统的监视工作,开发者需遵循如下指南: #### 添加依赖项 对于 Maven 用户来说,在项目的 `pom.xml` 中加入下面这段配置即可完成对 JNotify 的引入[^1]: ```xml <dependency> <groupId>net.contentobjects.jnotify</groupId> <artifactId>jnotify</artifactId> <version>0.94</version> </dependency> ``` #### 初始化设置 初始化过程中要指明想要跟踪的具体目录以及所关心的变化类型(如新增加、移除或者更新)。这里给出一段简单的实例代码展示怎样启动一个基本的文件观察器[^2]: ```java import net contentobjects jnotify; // 导入必要的包... public class FileWatcherExample { public static void main(String[] args) throws Exception { String path = "C:\\example"; // 设置目标文件夹 int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED; // 定义感兴趣的事件掩码 boolean watchSubtree = true; // 是否递归监测子文件夹内的变动 // 注册监听服务... int watchID = JNotify.addWatch(path, mask, watchSubtree, new JNotifyListener() { @Override public void fileCreated(int wd, String rootPath, String name) { System.out.println("File created: "+name); } @Override public void fileDeleted(int wd, String rootPath, String name) { System.out.println("File deleted: "+name); } @Override public void fileModified(int wd, String rootPath, String name) { System.out.println("File modified: "+name); } @Override public void fileRenamed(int wd, String oldName, String newName) { System.out.println("File renamed from "+oldName+" to "+newName); } }); Thread.sleep(Long.MAX_VALUE); // 阻塞主线程保持应用运行状态 } } ``` 上述例子展示了如何建立一个持续性的文件监视进程,并针对不同种类的操作定义对应的回调函数处理逻辑。当任何匹配条件的动作触发时,相应的方法就会被执行从而打印出有关的信息至控制台。 #### 关闭资源释放 值得注意的是,在不再需要继续追踪的情况下应当调用适当的方法停止监听并清理占用的资源。这可以通过调用 `removeWatch()` 方法传入之前获得的唯一标识符来实现[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值