Java监控文件变化


问题:
存在两个文件目录,且称之为源目录和目标目录,需要不定期将源目录和目标目录进行同步。
两种同步方法:
1 采用从源目录到目标目录的完全拷贝覆盖。显而易见的缺点,当文件目录中文件多、体积大时拷贝过程时间消耗极大。
2 采用从源目录到目标目录的变更集拷贝覆盖。避免了大量拷贝的IO耗时操作,但产生了新的问题:如何获取变更信息?

新问题:
如何监控一个文件目录的变更情况。
还是两种方法:
1 扫描式。不定期对源目录进行轮循扫描,获取变更。弱点:同样的,文件目录中文件多、体积大时扫描耗时久,响应也慢。
2 事件驱动式。当源目录发生变更时,抛出变更事件。JNI和JNotify可以提供支持,据说JDK 7内置支持,不过咱公司还没用上。

JNotify相关介绍:
JNotify:http://jnotify.sourceforge.net/,通过JNI技术,让Java代码可以实时的监控制定文件夹内文件的变动信息,支持Linux/Windows/MacOS。

JNotify的准备:
在使用JNotify之前,你需要“安装”一下JNotify,分为两个部分:jnotify-lib-0.93.jar和jnotify.dll/jnotify_64bit.dll。
jar自然设计类路径即可,dll则放置在java.library.path所指向的文件夹中。

java.library.path的值可以在java程序中通过如下语句:
System.getProperty("java.library.path")
查看,一般在windows下放在[jre安装目录]/bin下即可;
也可以手动指定程序的启动参数:
java -Djava.library.path=[dll路径]
的方法来达到目的;
也可以在java程序中通过如下语句:
System.load("xxxx/jnotify.dll")
来加载dll,这个可以方便程序打包。

JNotify使用了JNI技术来调用系统的本地库(Win下的是dll文件,Linux下是so文件),dll放置不正确,会有如下报错:
java.lang.UnsatisfiedLinkError: no jnotify in java.library.path 
    at java.lang.ClassLoader.loadLibrary(Unknown Source) 
    at java.lang.Runtime.loadLibrary0(Unknown Source) 
    at 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) 

JNotify使用示例:

  1. package com.dancen.test;  
  2.   
  3.   
  4. import net.contentobjects.jnotify.JNotify;  
  5. import net.contentobjects.jnotify.JNotifyListener;  
  6.   
  7.   
  8. public class FileWatch   
  9. {  
  10.     public static void main(String[] args)  
  11.     {  
  12.         try  
  13.         {  
  14.             new FileWatch().sampleTest();  
  15.         }  
  16.         catch (Exception e)  
  17.         {  
  18.             e.printStackTrace();  
  19.         }  
  20.     }  
  21.       
  22.     public void sampleTest() throws Exception  
  23.     {  
  24.         // path to watch   
  25.         String path = "D:\\download";  
  26.   
  27.   
  28.         // watch mask, specify events you care about,   
  29.         // or JNotify.FILE_ANY for all events.   
  30.         int mask = JNotify.FILE_CREATED  
  31.                 | JNotify.FILE_DELETED  
  32.                 | JNotify.FILE_MODIFIED  
  33.                 | JNotify.FILE_RENAMED;  
  34.   
  35.   
  36.         // watch subtree?   
  37.         boolean watchSubtree = true;  
  38.   
  39.   
  40.         // add actual watch   
  41.         int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());  
  42.   
  43.   
  44.         // sleep a little, the application will exit if you   
  45.         // don't (watching is asynchronous), depending on your   
  46.         // application, this may not be required   
  47.         Thread.sleep(1000000);  
  48.   
  49.   
  50.         // to remove watch the watch   
  51.         boolean res = JNotify.removeWatch(watchID);  
  52.           
  53.         if (!res)  
  54.         {  
  55.             // invalid watch ID specified.   
  56.         }  
  57.     }  
  58.   
  59.   
  60.     class Listener implements JNotifyListener   
  61.     {  
  62.         public void fileRenamed(int wd, String rootPath, String oldName, String newName)   
  63.         {  
  64.             print("renamed " + rootPath + " : " + oldName + " -> " + newName);  
  65.         }  
  66.   
  67.   
  68.         public void fileModified(int wd, String rootPath, String name)  
  69.         {  
  70.             print("modified " + rootPath + " : " + name);  
  71.         }  
  72.   
  73.   
  74.         public void fileDeleted(int wd, String rootPath, String name)  
  75.         {  
  76.             print("deleted " + rootPath + " : " + name);  
  77.         }  
  78.   
  79.   
  80.         public void fileCreated(int wd, String rootPath, String name)  
  81.         {  
  82.             print("created " + rootPath + " : " + name);  
  83.         }  
  84.   
  85.   
  86.         void print(String msg)   
  87.         {  
  88.             System.err.println(msg);  
  89.         }  
  90.     }  
  91. }  

相关内容