使用JNotify 监控文件变化

原文:https://blog.csdn.net/meteorsshower2013/article/details/80937725

其他参考文章:https://www.iteye.com/blog/cybrc-1900042

                         https://zhuanlan.zhihu.com/p/152229305

JNotify:文件系统事件的java库

JNotify是让应用程序监听文件系统事件的Java库,可以监听的事件例如:
创建文件事件
修改文件事件
文件重命名事件
删除文件事件

支持操作系统:
1.Windows
2.Linux
3.Max OS


JNotify使用

下载链接: http://jnotify.sourceforge.net/index.html官网地址

解压后目录:

测试使用

1.导入相关包

1.1 导入jar包
1.2 将jnotify_64bit.dll,jnotify.dll放入jrein目录中;我的jre目录是D:Program FilesJavajre-10.0.1in

 

简单实例

实现对文件目录的监听

import java.io.File;

import net.contentobjects.jnotify.JNotify;

public class FileNotify {
    public static void notify(String filepath) throws Exception {
        if (!new File(filepath).exists()) {
            System.out.println("文件目录不存在");
            return;
        }

        // 定义你所需要检测的事件类型,或者是全部FILE_ANY
        int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
        int mask1 =JNotify.FILE_ANY;
        System.out.println(JNotify.FILE_CREATED);//1
        System.out.println(JNotify.FILE_DELETED);//2
        System.out.println( JNotify.FILE_MODIFIED);//4
        System.out.println( JNotify.FILE_RENAMED);//8
        System.out.println(mask);//15
        System.out.println(mask1);//15

        // 是否检测子目录
        boolean watchSubtree = true;

        // 添加监听
        int watchID = JNotify.addWatch(filepath, mask, watchSubtree, new Listener());
        System.out.println(watchID);

        // 定义监听持续时间,此处是死循环,所以是时刻监听
//      while(true) {
//          Thread.sleep(1000*60);
//      }

        // 定义监听时间,如果超过这个时间,程序会退出;如果不定义就得不到监听
        Thread.sleep(1000*60);//60秒

        //移除监听
        boolean res = JNotify.removeWatch(watchID);
        if (!res) {
          // invalid watch ID specified.
        }

    }

    public static void main(String[] args) {
        String path ="F:\Pm25";
        System.out.println(path);
        try {
            notify(path);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

相关监听事件的处理

import net.contentobjects.jnotify.JNotifyListener;

class Listener implements JNotifyListener {
    public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
        System.out.println(wd);
        System.out.println(rootPath);
        print("renamed " + rootPath + " : " + oldName + " -> " + newName);
    }

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

    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);
    }
}

相关错误

如果出现如下错误,是因为缺少dll库

Error loading library, java.library.path=D:Program FilesJavajre-10.0.1in;C:windowsSunJavain;C:windowssystem32;C:windows;C:Program Files (x86)Common FilesOracleJavajavapath;C:windowssystem32;C:windows;C:windowsSystem32Wbem;C:windowsSystem32WindowsPowerShellv1.0;C:UsersAdministratorAppDataLocalMicrosoftWindowsApps;;.
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jnotify_64bit in java.library.path: [D:Program FilesJavajre-10.0.1in, C:windowsSunJavain, C:windowssystem32, C:windows, C:Program Files (x86)Common FilesOracleJavajavapath, C:windowssystem32, C:windows, C:windowsSystem32Wbem, C:windowsSystem32WindowsPowerShellv1.0\, C:UsersAdministratorAppDataLocalMicrosoftWindowsApps, ., .]
    at java.base/java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.base/java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.base/java.lang.System.loadLibrary(Unknown Source)
    at net.contentobjects.jnotify.win32.JNotify_win32.<clinit>(Unknown Source)
    at net.contentobjects.jnotify.win32.JNotifyAdapterWin32.<init>(Unknown Source)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.base/java.lang.Class.newInstance(Unknown Source)
    at net.contentobjects.jnotify.JNotify.<clinit>(Unknown Source)
    at FileNotify.notify(FileNotify.java:26)
    at FileNotify.main(FileNotify.java:49)

解决:
1.将jnotify_64bit.dll,jnotify.dll放入jrein目录

友情提示:

Java有两个Path,一个是classpath,另外一个library.path。

classpath是设置JDK的lib位置.

而library.path是设置引用的非Java类包(如DLL,SO)的位置。

 

此类问题设置参见: 百度链接

解决方案:使用JNotify

JNotify:文件系统事件的java库


JNotify介绍

JNotify是让应用程序监听文件系统事件的Java库,可以监听的事件例如:
创建文件事件
修改文件事件
文件重命名事件
删除文件事件

支持操作系统:
1.Windows
2.Linux
3.Max OS


JNotify使用

下载链接: http://jnotify.sourceforge.net/index.html官网地址

解压后目录:
解压目录

测试使用

1.导入相关包

1.1 导入jar包
1.2 将jnotify_64bit.dll,jnotify.dll放入jrein目录中;我的jre目录是D:Program FilesJavajre-10.0.1in
这里写图片描述

简单实例

实现对文件目录的监听

import java.io.File;

import net.contentobjects.jnotify.JNotify;

public class FileNotify {
    public static void notify(String filepath) throws Exception {
        if (!new File(filepath).exists()) {
            System.out.println("文件目录不存在");
            return;
        }

        // 定义你所需要检测的事件类型,或者是全部FILE_ANY
        int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
        int mask1 =JNotify.FILE_ANY;
        System.out.println(JNotify.FILE_CREATED);//1
        System.out.println(JNotify.FILE_DELETED);//2
        System.out.println( JNotify.FILE_MODIFIED);//4
        System.out.println( JNotify.FILE_RENAMED);//8
        System.out.println(mask);//15
        System.out.println(mask1);//15

        // 是否检测子目录
        boolean watchSubtree = true;

        // 添加监听
        int watchID = JNotify.addWatch(filepath, mask, watchSubtree, new Listener());
        System.out.println(watchID);

        // 定义监听持续时间,此处是死循环,所以是时刻监听
//      while(true) {
//          Thread.sleep(1000*60);
//      }

        // 定义监听时间,如果超过这个时间,程序会退出;如果不定义就得不到监听
        Thread.sleep(1000*60);//60秒

        //移除监听
        boolean res = JNotify.removeWatch(watchID);
        if (!res) {
          // invalid watch ID specified.
        }

    }

    public static void main(String[] args) {
        String path ="F:\Pm25";
        System.out.println(path);
        try {
            notify(path);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

相关监听事件的处理

import net.contentobjects.jnotify.JNotifyListener;

class Listener implements JNotifyListener {
    public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
        System.out.println(wd);
        System.out.println(rootPath);
        print("renamed " + rootPath + " : " + oldName + " -> " + newName);
    }

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

    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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

相关错误

如果出现如下错误,是因为缺少dll库

Error loading library, java.library.path=D:Program FilesJavajre-10.0.1in;C:windowsSunJavain;C:windowssystem32;C:windows;C:Program Files (x86)Common FilesOracleJavajavapath;C:windowssystem32;C:windows;C:windowsSystem32Wbem;C:windowsSystem32WindowsPowerShellv1.0;C:UsersAdministratorAppDataLocalMicrosoftWindowsApps;;.
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jnotify_64bit in java.library.path: [D:Program FilesJavajre-10.0.1in, C:windowsSunJavain, C:windowssystem32, C:windows, C:Program Files (x86)Common FilesOracleJavajavapath, C:windowssystem32, C:windows, C:windowsSystem32Wbem, C:windowsSystem32WindowsPowerShellv1.0\, C:UsersAdministratorAppDataLocalMicrosoftWindowsApps, ., .]
    at java.base/java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.base/java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.base/java.lang.System.loadLibrary(Unknown Source)
    at net.contentobjects.jnotify.win32.JNotify_win32.<clinit>(Unknown Source)
    at net.contentobjects.jnotify.win32.JNotifyAdapterWin32.<init>(Unknown Source)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.base/java.lang.Class.newInstance(Unknown Source)
    at net.contentobjects.jnotify.JNotify.<clinit>(Unknown Source)
    at FileNotify.notify(FileNotify.java:26)
    at FileNotify.main(FileNotify.java:49)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

解决:
1.将jnotify_64bit.dll,jnotify.dll放入jrein目录

友情提示:

Java有两个Path,一个是classpath,另外一个library.path。

classpath是设置JDK的lib位置.

而library.path是设置引用的非Java类包(如DLL,SO)的位置。

原文地址:https://www.cnblogs.com/shihaiming/p/13466102.html