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();
    }
}

你可能感兴趣的:(java,python,开发语言)