java实现文件监听--创建、修改、删除

亲测可用

package cn.xd.dcol.railway.business.receiveData;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import java.io.File;

@Slf4j
public class TimsFileListener extends FileAlterationListenerAdaptor {
    /**
     * File system observer started checking event.(文件系统观察者开始检查事件)
     *
     * @param observer The file system observer (ignored)
     */
    @Override
    public void onStart(FileAlterationObserver observer) {
        super.onStart(observer);
    }

    /**
     * Directory created Event.
     *
     * @param directory The directory created (ignored)
     */
    @Override
    public void onDirectoryCreate(File directory) {
        log.info("创建文件夹 : {}",directory.getAbsolutePath());
    }

    /**
     * Directory changed Event.
     *
     * @param directory The directory changed (ignored)
     */
    @Override
    public void onDirectoryChange(File directory) {
        log.info("修改文件夹 : {}",directory.getAbsolutePath());
    }

    /**
     * Directory deleted Event.
     *
     * @param directory The directory deleted (ignored)
     */
    @Override
    public void onDirectoryDelete(File directory) {
        log.info("删除文件夹 : {}",directory.getAbsolutePath());
    }

    /**
     * File created Event.
     *
     * @param file The file created (ignored)
     */
    @SneakyThrows
    @Override
    public void onFileCreate(File file) {
        log.info("创建文件 : {}",file.getAbsolutePath());
        log.info("创建文件 : {}",file.getParentFile().getName());
        log.info("创建文件 : {}",file.getName());
        String parentName=file.getParentFile().getName();
        /*if(parentName.equals("aaa")){
            System.out.println("=======》执行确保信息");
            ReceiveDataToJson.receiveTimsData(file.getAbsolutePath());
        }
        if(parentName.equals("超偏载")){
            System.out.println("=======》执行超偏载信息");
            ReceiveDataToJson.receiveOverLoadData(file.getAbsolutePath(),file.getName());

        }
        if(parentName.equals("车号识别")){
            System.out.println("=======》执行车号识别信息");
            ReceiveDataToJson.receiveTrainNoData(file.getAbsolutePath(),file.getName());
        }*/
    }

    /**
     * File changed Event.
     *
     * @param file The file changed (ignored)
     */
    @Override
    public void onFileChange(File file) {
        log.info("修改文件 : {}",file.getAbsolutePath());
    }

    /**
     * File deleted Event.
     *
     * @param file The file deleted (ignored)
     */
    @Override
    public void onFileDelete(File file) {
        log.info("删除文件 : {}",file.getAbsolutePath());
    }

    /**
     * File system observer finished checking event.(文件系统观察者已完成检查事件)
     *
     * @param observer The file system observer (ignored)
     */
    @Override
    public void onStop(FileAlterationObserver observer) {
        super.onStop(observer);
    }
}

package cn.xd.dcol.railway.business.receiveData;

import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class TrainOverLoadExecuteListener implements CommandLineRunner {
    @Override
    public synchronized  void run(String... args) throws Exception {
        System.out.println("CommandLineRunner:开始运行...");
        String rootDir = "D:/Users/Desktop/桌面资料/物流系统资料/铁运资料/超偏载";
        //Interval time 5 seconds(间隔时间5秒)
        long intervalTime = TimeUnit.SECONDS.toMillis(5);

        FileAlterationObserver observer2 = new FileAlterationObserver(rootDir, FileFilterUtils.and(
                FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter(".success")));

        FileAlterationObserver observer = new FileAlterationObserver(rootDir);

        //Set file change listener
        observer.addListener(new TimsFileListener());
        //Create file change monitor
        FileAlterationMonitor monitor = new FileAlterationMonitor(intervalTime, observer);
        //start monitor
        monitor.start();
        //设置关闭时间
        //Thread.sleep(3000);
        //monitor.stop();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值