Java中监控文件变化的多种方案

news/2024/7/6 1:47:30

一、使用Apache.Common.io库

package yungoal.huafeng.utils.files;

import com.sun.deploy.util.SyncFileAccess;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

import java.io.File;

public class FolderWatcher extends FileAlterationListenerAdaptor {
    FileAlterationMonitor monitor;

    public FolderWatcher(String directory) {
        monitor = new FileAlterationMonitor(500);
        FileAlterationObserver fileObserver = new FileAlterationObserver(directory);
        fileObserver.addListener(this);
        monitor.addObserver(fileObserver);
    }

    public void start(boolean isDaemonThread) throws Exception {
        if (isDaemonThread) {
            monitor.setThreadFactory(r -> {
                Thread th = new Thread(r);
                th.setDaemon(true);
                return th;
            });
        }
        monitor.start();
    }

    @Override
    public void onFileChange(File file) {
        System.out.println("change" + file);
    }

    @Override
    public void onFileCreate(File file) {
        while (true) {
            try {
                SyncFileAccess fileAccess = new SyncFileAccess(file);
                SyncFileAccess.FileInputStreamLock lock = fileAccess.openLockFileInputStream(1000, false);lock.getFileInputStream().close();
          lock.release();
          
break;
        }
catch (Exception e)
       {
try { Thread.sleep(100); } catch (InterruptedException ignored) { } } } System.out.println("create:" + file); } }

二、使用JAVA.NIO的

package yungoal.huafeng.utils.files;

import java.io.IOException;
import java.nio.file.*;

import static java.nio.file.StandardWatchEventKinds.*;

public class FolderWatcher2 {
    private WatchService watcher;

    public FolderWatcher2(String directory) throws IOException {
        watcher = FileSystems.getDefault().newWatchService();
        Path path = Paths.get(directory);
        path.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);
        Path path1 = Paths.get(directory, "abc");
        path1.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);
    }
    public void start() throws InterruptedException {
        while (true) {
            WatchKey key = watcher.take();
            for (WatchEvent<?> event : key.pollEvents()) {
                WatchEvent.Kind<?> kind = event.kind();
                if (kind == OVERFLOW) {//事件可能lost or discarded
                    continue;
                }

                Path fileName = (Path) event.context();
                System.out.printf("Event %s has happened,which fileName is %s%n", kind.name(), fileName);
            }
            //这行必须有,不然不能连续地监控目录
            if (!key.reset()) {
                break;
            }
        }
    }

}

 


http://www.niftyadmin.cn/n/1934959.html

相关文章

安卓凹凸自定义View

这个是产品的效果图然后实际运行的结果那到这个需求感觉还是很简单的&#xff0c;让美术出了一张图&#xff0c;然后我把这个背景图做成了.9图&#xff0c;然而&#xff0c;并没有什么卵用&#xff0c;最大的原因就是background被拉伸、挤压&#xff0c;高度在不同的机型显示的…

网站一直优化不动,是不是被惩罚?

SEO是一个千变万化的工作&#xff0c;你的每一个优化策略&#xff0c;都决定网站不同的走向&#xff0c;在很长一段时期中&#xff0c;我们通常会遇到各种问题&#xff0c;比如&#xff1a;网站一直优化不动。 长期停留在某个阶段&#xff0c;这让SEO人员&#xff0c;显得无所…

leetcode算法题解(Java版)-2-最长回文子串

一、int数字反转 题目描述Reverse digits of an integer. Example1: x 123, return 321Example2: x -123, return -321 思路&#xff1a; 题目很简单&#xff0c;需要注意的是&#xff1a;int型是32位的。1000000003 反转后就超了&#xff01;所以需要包装类Integer中的最大值…

浅谈 串行信号 转换成 并行信号 原理

注 &#xff1a; 文中讲述的原理是推理和探讨 &#xff0c; 和现实中的实现不一定完全相同 。 开始之前&#xff0c; 可以先参考看看我之前写的两篇文章 &#xff1a; 《设计一个 硬件 实现的 Dictionary&#xff08;字典&#xff09;》 https://www.cnblogs.com/KSongKing/p…

python学习之路——命名关键字参数、函数对象、函数嵌套、名称空间与作用域、闭包函数...

命名关键字参数&#xff1a;在定义函数时&#xff0c;*与**之间参数称之为命名关键字参数 特点&#xff1a;在调用函数时&#xff0c;命名关键字参数必须按照keyvalue的形式传值 函数对象 函数是第一类对象&#xff1a;指的是函数的内存地址可以像一个变量值一样去使用 1.变量值…

小程序的授权机制

小程序的授权机制 it修真院小课堂 目录 1.背景介绍 2.知识剖析 3.常见问题 4.解决方案 5.编码实战 6.扩展思考 7.参考文献 8.更多讨论 1.背景介绍 说到这&#xff0c;我们需要先了解一下微信小程序是啥&#xff1f;简单说&#xff0c;它就是一个可以实现之前只能是原生态APP可以…

敏捷AI|NLP技术在宜信业务中的实践「背景篇」

导读&#xff1a;文章围绕基于机器学习的NLP技术在宜信内部各业务领域的应用实践展开&#xff0c;分享这一过程中的相关经验&#xff0c;包括智能机器人在业务支持、客户服务中的探索&#xff0c;基于文本语义分析的用户画像构建&#xff0c;以及NLP算法服务平台化实施思路等。…

[翻译] Google的javascript风格指南中值得注意的13点

写在文章前 这篇文章翻译自13 Noteworthy Points from Google’s JavaScript Style Guide . 如果觉得风格指南的完整版太长的话&#xff0c;这篇文章提取出来了比较值得注意的几点&#xff0c;比如说tab vs space这种堪比咸甜豆腐脑的战争。&#xff08;还记得美剧硅谷中&#…