java 监听jnotify.dll_使用JNotify 监控文件变化

本文介绍了如何使用JNotify这个Java库来监听文件系统的变化,包括创建、删除、修改和重命名文件事件。在Windows、Linux和Mac OS上皆可运行。通过将jnotify_64bit.dll和jnotify.dll放入jre/bin目录并设置好library.path,可以解决相关错误。
摘要由CSDN通过智能技术生成

JNotify:文件系统事件的java库

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

支持操作系统:1.Windows2.Linux3.Max OS

JNotify使用

解压后目录:

04a6479bd7eb114faa66e8c93468d1ed.png

测试使用

1.导入相关包

1.1 导入jar包1.2 将jnotify_64bit.dll,jnotify.dll放入jre\bin目录中;我的jre目录是D:\Program Files\Java\jre-10.0.1\bin

2db73e02607505542c79fb2fcf67db79.png

简单实例

实现对文件目录的监听

importjava.io.File;importnet.contentobjects.jnotify.JNotify;public classFileNotify {public static void notify(String filepath) throwsException {if (!newFile(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, newListener());

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 voidmain(String[] args) {

String path="F:\\Pm25";

System.out.println(path);try{

notify(path);

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

相关监听事件的处理

importnet.contentobjects.jnotify.JNotifyListener;class Listener implementsJNotifyListener {public void fileRenamed(intwd, String rootPath, String oldName, String newName) {

System.out.println(wd);

System.out.println(rootPath);

print("renamed " + rootPath + " : " + oldName + " -> " +newName);

}public void fileModified(intwd, String rootPath, String name) {

print("modified " + rootPath + " : " +name);

}public void fileDeleted(intwd, String rootPath, String name) {

print("deleted " + rootPath + " : " +name);

}public void fileCreated(intwd, String rootPath, String name) {

print("created " + rootPath + " : " +name);

}voidprint(String msg) {

System.err.println(msg);

}

}

相关错误

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

Error loading library, java.library.path=D:\Program Files\Java\jre-10.0.1\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;;.

Exception in thread"main" java.lang.UnsatisfiedLinkError: no jnotify_64bit in java.library.path: [D:\Program Files\Java\jre-10.0.1\bin, C:\windows\Sun\Java\bin, C:\windows\system32, C:\windows, C:\Program Files (x86)\Common Files\Oracle\Java\javapath, C:\windows\system32, C:\windows, C:\windows\System32\Wbem, C:\windows\System32\WindowsPowerShell\v1.0\, C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps, ., .]

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.(Unknown Source)

at net.contentobjects.jnotify.win32.JNotifyAdapterWin32.(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.(Unknown Source)

at FileNotify.notify(FileNotify.java:26)

at FileNotify.main(FileNotify.java:49)

解决:1.将jnotify_64bit.dll,jnotify.dll放入jre\bin目录

友情提示:

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

classpath是设置JDK的lib位置.

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

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

解决方案:使用JNotify

JNotify:文件系统事件的java库

JNotify介绍

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

支持操作系统:1.Windows2.Linux3.Max OS

JNotify使用

解压后目录:

246500c4d29e90e5b533693713ec7f3a.png

测试使用

1.导入相关包

1.1 导入jar包1.2 将jnotify_64bit.dll,jnotify.dll放入jre\bin目录中;我的jre目录是D:\Program Files\Java\jre-10.0.1\bin

d7647e21753295347b6da341b28195dd.png

简单实例

实现对文件目录的监听

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 Files\Java\jre-10.0.1\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;;.

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jnotify_64bit in java.library.path: [D:\Program Files\Java\jre-10.0.1\bin, C:\windows\Sun\Java\bin, C:\windows\system32, C:\windows, C:\Program Files (x86)\Common Files\Oracle\Java\javapath, C:\windows\system32, C:\windows, C:\windows\System32\Wbem, C:\windows\System32\WindowsPowerShell\v1.0\, C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps, ., .]

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.(Unknown Source)

at net.contentobjects.jnotify.win32.JNotifyAdapterWin32.(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.(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放入jre\bin目录

友情提示:

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

classpath是设置JDK的lib位置.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值