使用JNotify 监控文件变化

摘要:
原文:https://blog.csdn.net/meteorsshower2013/article/details/80937725其他参考文章:https://www.iteye.com/blog/cybrc-1900042https://zhuanlan.zhihu.com/p/152229305JNotify:文件系统事件的Java库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官网地址

解压后目录:

使用JNotify 监控文件变化第1张

测试使用

1.导入相关包

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

 

使用JNotify 监控文件变化第2张

简单实例

实现对文件目录的监听

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)的位置。

免责声明:文章转载自《使用JNotify 监控文件变化》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇js调用百度语音合成测usb读写下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Java SPI机制学习笔记

最近在阅读框架源代码时,常常看到 SPI 的子包, 忍不住查了下: Service Provider Interface : 服务提供接口。 JavaSPI 实际上是“基于接口的编程+策略模式+配置文件”组合实现的动态加载机制。具体而言: STEP1. 定义一组接口, 假设是 autocomplete.PrefixMatcher; STEP2. 写出接口的...

makefile中的自动化变量 【转】

转自:http://blog.chinaunix.net/uid-28458801-id-3495215.html 自动化变量 模式规则中,规则的目标和依赖文件名代表了一类文件名;规则的命令是对所有这一类文件重建过程的描述,显然,在命令中不能出现具体的文件名,否则模式规则失去意义。那么在模式规则的命令行中该如何表示文件,将是本小节的讨论的重点。 假如你需要...

hbase安装配置(整合到hadoop)

如果想详细了解hbase的安装:http://abloz.com/hbase/book.html 和官网http://hbase.apache.org/ 1.  快速单击安装 在单机安装Hbase的方法。会引导你通过shell创建一个表,插入一行,然后删除它,最后停止Hbase。只要10分钟就可以完成以下的操作。 1.1下载解压最新版本 选择一个 ...

Python打包工具

当我们完成一个完整的项目,需要该项目文件中打包成分发包共享给他人或者上传到pypi社区以供他人下载。这就需要对该项目进行打包分发。 项目文件 这是一个已写好的项目文件包,叫做my_pkg,它的目录结构如下: tutorial/ my_pkg/ __init__.py main.py utils/...

JUC 并发编程--04 常用的辅助类CountDownLatch , CyclicBarrier , Semaphore , 读写锁 , 阻塞队列,CompletableFuture(异步回调)

CountDownLatch 相当于一个减法计数器, 构造方法指定一个数字,比如6, 一个线程执行一次,这个数字减1, 当变为0 的时候, await()方法,才开始往下执行,, 看这个例子 CyclicBarrier 的用法, 字面意思:循环栅栏, 这是构造方法, 第一个参数parties 是线程数量, 第二个参数是barrierAction:...

【逆向】Yara规则编写安装与使用教程

前言 Yara是一个能够帮助恶意软件研究人员识别和分类恶意软件样本的工具(类似正则表达式)。规则可以通过文本或二进制的模式被创建,并且每个规则均由一组字符串和一个布尔表达式组成。 1 //示例规则 2 rule Test : Trojan 3 { 4 //规则描述 5 meta: 6 author = "Sunset" 7...