• 使用apache common-io 监控文件变化


    package common.io;
    
    
    import org.apache.commons.io.filefilter.FileFilterUtils;
    import org.apache.commons.io.monitor.FileAlterationListener;
    import org.apache.commons.io.monitor.FileAlterationMonitor;
    import org.apache.commons.io.monitor.FileAlterationObserver;
    
    
    import java.io.File;
    import java.util.concurrent.TimeUnit;
    
    
    /**
     * Created with IntelliJ IDEA.
     * User: superman
     * Date: 14-1-5
     * Time: 上午11:43
     * To change this template use File | Settings | File Templates.
     */
    public class Example3 {
        public static void main(String[] args) throws Exception{
            File directory = new File("D:/test");
            // 轮询间隔 5 秒
            long interval = TimeUnit.SECONDS.toMillis(5);
            // 创建一个文件观察器用于处理文件的格式
            FileAlterationObserver observer = new FileAlterationObserver(directory, FileFilterUtils.and(
                    FileFilterUtils.fileFileFilter(),FileFilterUtils.suffixFileFilter(".txt")));
            //设置文件变化监听器
            observer.addListener(new MyFileListener());
            FileAlterationMonitor monitor = new FileAlterationMonitor(interval,observer);
            monitor.start();
            //Thread.sleep(30000);
            //monitor.stop();
        }
    }
    

      

    final class MyFileListener implements FileAlterationListener{
        @Override
        public void onStart(FileAlterationObserver fileAlterationObserver) {
            System.out.println("monitor start scan files..");
        }
    
    
        @Override
        public void onDirectoryCreate(File file) {
            System.out.println(file.getName()+" director created.");
        }
    
    
        @Override
        public void onDirectoryChange(File file) {
            System.out.println(file.getName()+" director changed.");
        }
    
    
        @Override
        public void onDirectoryDelete(File file) {
            System.out.println(file.getName()+" director deleted.");
        }
    
    
        @Override
        public void onFileCreate(File file) {
            System.out.println(file.getName()+" created.");
        }
    
    
        @Override
        public void onFileChange(File file) {
            System.out.println(file.getName()+" changed.");
        }
    
    
        @Override
        public void onFileDelete(File file) {
            System.out.println(file.getName()+" deleted.");
        }
    
    
        @Override
        public void onStop(FileAlterationObserver fileAlterationObserver) {
            System.out.println("monitor stop scanning..");
        }
    }
    

      

  • 相关阅读:
    ES6-01 2018-02-06
    8.1 IO类
    2.4 const限定符
    C++命名空间
    win7系统docker安装ubuntu
    win7安装docker
    wuzhicms 查看模板中的所有可用变量和值
    wuzhicms上传弹出层,如何返回数据到当前页面?
    wuzhicms 无规律推荐位标签的嵌套使用
    【wuzhicms】apache 设置禁止访问某些文件或目录
  • 原文地址:https://www.cnblogs.com/xiohao/p/5561340.html
Copyright © 2020-2023  润新知