JNotify文件变化通知



JNotify,一个支持动态监控文件和文件夹(支持级联监控)的架包。在windows中,需要添加附件的dll文件,因为windows默认没有该服务,这是大拿们自己开发的一个功能。 


1,将jnotify包引入到工程中。
2,将jnotify依赖的dll文件加入到java.library.path路径中。这个变量可能会有多个位置,随便将jnotify压缩包中附带的libjnotify.so文件加入到其中的任何一个路径中即可。如果不知道这个变量的值,可以使用System.getProperty("java.library.path")查看。


package org.liufeng.weixin.util;

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;

public class Test {

 public static void main(String[] args) {
  try {
   new Test().sample();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 


 public void sample() throws Exception {
  // path to watch
  String path = "D:/logs";

  // watch mask, specify events you care about,
  // or JNotify.FILE_ANY for all events.
 
  int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED
    | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;

  // watch subtree?
  boolean watchSubtree = true;

  // add actual watch
  int watchID = JNotify
    .addWatch(path, mask, watchSubtree, new Listener());

  // sleep a little, the application will exit if you
  // don't (watching is asynchronous), depending on your
  // application, this may not be required
  Thread.sleep(1000000);

  // to remove watch the watch
  boolean res = JNotify.removeWatch(watchID);
  if (!res) {
   // invalid watch ID specified.
  }
 }

        //可以在下面的监控方法中添加自己的代码。比如在fileModified中添加重新加载配置文件的代码
 class Listener implements JNotifyListener {
  public void fileRenamed(int wd, String rootPath, String oldName,
    String newName) {
   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);
  }

 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JNotify是一个Java库,它允许您在操作系统上监视文件系统事件。它支持多个平台,包括Linux。 要在Linux中使用JNotify,请按照以下步骤操作: 1. 下载JNotify的最新版本,例如jnotify-0.94.zip。 2. 解压缩下载的文件,例如将其解压缩到/usr/local/jnotify目录中。 3. 在您的Java项目中添加JNotify库。您可以将JNotify.jar文件添加到您的类路径中。 4. 在您的Java代码中使用JNotify API。以下是一个简单的示例: ``` import net.contentobjects.jnotify.*; public class JNotifyTest { public static void main(String[] args) throws Exception { String path = "/path/to/monitor"; int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED; boolean watchSubtree = true; JNotify.addWatch(path, mask, watchSubtree, new Listener()); Thread.sleep(1000000); } static class Listener implements JNotifyListener { public void fileCreated(int wd, String rootPath, String name) { System.out.println("Created " + rootPath + " : " + name); } public void fileDeleted(int wd, String rootPath, String name) { System.out.println("Deleted " + rootPath + " : " + name); } public void fileModified(int wd, String rootPath, String name) { System.out.println("Modified " + rootPath + " : " + name); } public void fileRenamed(int wd, String rootPath, String oldName, String newName) { System.out.println("Renamed " + rootPath + " : " + oldName + " -> " + newName); } } } ``` 此代码将监视指定路径中的文件系统事件,并在事件发生时输出日志消息。您可以使用不同的事件掩码和监听器来处理其他类型的事件。 5. 运行您的Java应用程序。在Linux上,您可能需要以root用户身份运行您的应用程序才能监视系统级别的文件系统事件。 希望这可以帮助您在Linux中使用JNotify
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值