https://blog.csdn.net/codepython/article/details/42341243?utm_source=blogxgwz1
使用jnotify
https://blog.csdn.net/stalin_/article/details/80234979
下载资源,添加maven导入
开发时把jnotify_64bit.dll(64位系统用)扔到jdk的jre文件夹下bin目录比如C:\Program Files\Java\jdk1.8.0_201\jre\bin下
如果是部署,参照rxtx一文https://www.cnblogs.com/jnhs/p/10274667.html
应该把dll放在C:\WINDOWS\system32下才可
新建一个util包
新建一个Jnotify类
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.hs.util; import net.contentobjects.jnotify.JNotify; import net.contentobjects.jnotify.JNotifyAdapter; import net.contentobjects.jnotify.JNotifyException; public class Jnotify extends JNotifyAdapter { private static final String REQUEST_BASE_PATH = "D:\\csdnautodl\\tempfile"; /** * 被监视的目录 */ String path = REQUEST_BASE_PATH; /** * 关注目录的事件 */ int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED; /** * 是否监视子目录,即级联监视 */ boolean watchSubtree = true; /** * 监听程序Id */ public int watchID; /** * 容器启动时启动监视程序 * * */ public void beginWatch() { /** * 添加到监视队列中 */ try { this.watchID = JNotify.addWatch(path, mask, watchSubtree, this); System.err.println("jnotify -----------启动成功-----------"); } catch (JNotifyException e) { e.printStackTrace(); } /** * 死循环,线程一直执行,休眠一分钟后继续执行,主要是为了让主线程一直执行 休眠时间和监测文件发生的效率无 * (就是说不是监视目录文件改变一分钟后才监测到,监测几乎是实时的,调用本地系统库) * */ while (true) { try { Thread.sleep(60000); } catch (InterruptedException e) {// ignore it } } } /** * 当监听目录下一旦有新的文件被创建,则即触发该事件 * * @param wd 监听线程id * @param rootPath 监听目录 * @param name 文件名称 */ @Override public void fileCreated(int wd, String rootPath, String name) { System.err.println("文件被创建, 创建位置为: " + rootPath + "/" + name); } @Override public void fileRenamed(int wd, String rootPath, String oldName, String newName) { System.err.println("文件被重命名, 原文件名为:" + rootPath + "/" + oldName + ", 现文件名为:" + rootPath + "/" + newName); } @Override public void fileModified(int wd, String rootPath, String name) { System.err.println("文件内容被修改, 文件名为:" + rootPath + "/" + name); } @Override public void fileDeleted(int wd, String rootPath, String name) { System.err.println("文件被删除, 被删除的文件名为:" + rootPath + name); } }
然后在窗体程序执行的时候开始启动监视