zoukankan      html  css  js  c++  java
  • 使用JNotify监控目录下文件变更

    项目需求,监控文件夹下,文件的变更。 — JNotify

    JNotify,跨平台,支持Linux,win32, win64,mac

    原理:略。

    下载地址:去sourceforge下载

    使用方法:

    [1]下载JNotify包, 里面有linux,windows,mac平台,所使用的包。其中windows平台使用dll文件,linux平台使用so结尾的文件

    [2]以windows平台为例。建javase项目。

    [3]引入JNotify.jar包

    [4]将JNotify.dll文件,放在电脑随便某个目录下,例如 c:/mydll/JNotify.dll

    [5]告诉程序,在执行程序时,去哪里调用dll文件(linux下是so文件)。

    方法,右键项目,debug as  — > debug configuration

     

    [6]建第一个类,作为主程序

    [java] view plain copy
     
    1. package me.demo;  
    2.    
    3. import net.contentobjects.jnotify.JNotify;  
    4. import net.contentobjects.jnotify.JNotifyException;  
    5.    
    6. public class JNotifyTest {  
    7.     public static void main(String[] args) {  
    8.         System.err.println(System.getProperty("java.library.path"));  
    9.         System.err.println("开始监听目录下内容......");  
    10.         try {  
    11.             JNotifyTest.sample();  
    12.         } catch (Exception e) {  
    13.             e.printStackTrace();  
    14.         }  
    15.     }  
    16.    
    17.     /** 
    18.      * JNotify监控方法 
    19.      * @throws JNotifyException 
    20.      * @throws InterruptedException 
    21.      */  
    22.     private static void sample() throws JNotifyException, InterruptedException {  
    23.    
    24.         //要监控哪个目录  
    25.         String path = "/var/alldata/";  
    26.    
    27.         //监控用户的操作,增,删,改,重命名  
    28.         int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED ;  
    29.    
    30.         //是否监控子目录  
    31.         boolean subTree = true;  
    32.    
    33.         //开始监控  
    34.         int watchID = JNotify.addWatch(path, mask, subTree, new MyJNotifyListener());  
    35.    
    36.         //睡一会,看看效果  
    37.         Thread.sleep(1000 * 60 * 3);  
    38.    
    39.         //停止监控  
    40.         boolean res = JNotify.removeWatch(watchID);  
    41.    
    42.         if (res) {  
    43.             System.err.println("已停止监听");  
    44.         }  
    45.         System.err.println(path);  
    46.     }  
    47.    
    48. //  public static class Listener implements JNotifyListener{  
    49. //      @Override  
    50. //      public void fileCreated(int wd, String rootPath, String name) {  
    51. //          System.err.println("create: --->" + wd + "--->" + rootPath + "--->" + name);  
    52. //      }  
    53. //      @Override  
    54. //      public void fileDeleted(int wd, String rootPath, String name) {  
    55. //          System.err.println("create: --->" + wd + "--->" + rootPath + "--->" + name);  
    56. //      }  
    57. //      @Override  
    58. //      public void fileModified(int wd, String rootPath, String name) {  
    59. //          System.err.println("create: --->" + wd + "--->" + rootPath + "--->" + name);  
    60. //      }  
    61. //      @Override  
    62. //      public void fileRenamed(int wd, String rootPath, String oldName, String newName) {  
    63. //          System.err.println("create: --->" + wd + "--->" + rootPath + "--->" + oldName + "--->" + newName);  
    64. //      }  
    65. //  }  
    66. }  

    [7]建第二个类,实现JNotifyListener接口。这个类作用就是,检测到目录修改后,要做哪些动作。

    [python] view plain copy
     
      1. package me.demo;  
      2.    
      3. import net.contentobjects.jnotify.JNotifyListener;  
      4.    
      5. public class MyJNotifyListener implements JNotifyListener{  
      6.   
      7.     @Override  
      8.     public void fileCreated(int wd, String rootPath, String name) {  
      9.         System.err.println("create: --->" + wd + "--->" + rootPath + "--->" + name);  
      10.     }  
      11.   
      12.     @Override  
      13.     public void fileDeleted(int wd, String rootPath, String name) {  
      14.         System.err.println("delete: --->" + wd + "--->" + rootPath + "--->" + name);  
      15.     }  
      16.   
      17.     @Override  
      18.     public void fileModified(int wd, String rootPath, String name) {  
      19.         System.err.println("modified: --->" + wd + "--->" + rootPath + "--->" + name);  
      20.     }  
      21.   
      22.     @Override  
      23.     public void fileRenamed(int wd, String rootPath, String oldName, String newName) {  
      24.         System.err.println("rename: --->" + wd + "--->" + rootPath + "--->" + oldName + "--->" + newName);  
      25.     }  
      26. }  
  • 相关阅读:
    HTTP——Web服务器、代理、缓存
    nginx配置文件详解2
    nginx配置文件详解
    shell笔记2
    django笔记
    python 发请求,urllib,urllib2
    nginx配置
    python os模块学习
    mac 终端命令小结
    mac常用命令笔记
  • 原文地址:https://www.cnblogs.com/xxj-bigshow/p/9174560.html
Copyright © 2011-2022 走看看