zoukankan      html  css  js  c++  java
  • 使用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..");
        }
    }
    

      

  • 相关阅读:
    反编译
    字符编码集格式
    BZOJ 1032 [JSOI2007]祖码Zuma
    2015-7-21 模板练习
    2015-7-20 模板练习
    BZOJ 1028 [JSOI2007]麻将
    BZOJ 1027 [JSOI2007]合金
    BZOJ 1026 [SCOI2009]windy数
    BZOJ 1025 [SCOI2009]游戏
    COJ 2024 仙境传奇(五)——一个天才的觉醒 素数筛
  • 原文地址:https://www.cnblogs.com/xiohao/p/5561340.html
Copyright © 2011-2022 走看看