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

一、使用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;
            }
        }
    }

}

 

转载于:https://www.cnblogs.com/songxingzhu/p/8961491.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值