zoukankan      html  css  js  c++  java
  • common-io 文件监听例子

    package com.junge.spring.demo.commonio;
    
    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;
    
    /**
     * 文件监控工具
     *
     * @author liuxj
     * @create 2018-02-11 22:02
     */
    public class FileMonitorTest {
    
        public static void main(String[] args) throws Exception {
    
            MyFileLister myFileLister = new MyFileLister();
            FileAlterationObserver observer1 = new FileAlterationObserver("I:\test");
            observer1.addListener(myFileLister);
    
            FileAlterationMonitor monitor = new FileAlterationMonitor(10000);
            monitor.addObserver(observer1);
    
            monitor.start();
    
            //monitor.stop(); //停止监听
        }
    }
    
    class MyFileLister extends FileAlterationListenerAdaptor {
        @Override
        public void onStart(FileAlterationObserver observer) {
            System.out.println("onStart");
        }
    
        @Override
        public void onDirectoryCreate(File directory) {
            System.out.println("onDirectoryCreate");
        }
    
        @Override
        public void onDirectoryChange(File directory) {
            System.out.println("onDirectoryChange");
        }
    
        @Override
        public void onDirectoryDelete(File directory) {
            System.out.println("onDirectoryDelete");
        }
    
        @Override
        public void onFileCreate(File file) {
            System.out.println("onFileCreate,file=" + file.getName());
        }
    
        @Override
        public void onFileChange(File file) {
            System.out.println("onFileChange,file=" + file.getName());
        }
    
        @Override
        public void onFileDelete(File file) {
            System.out.println("onFileDelete,file=" + file.getName());
        }
    
        @Override
        public void onStop(FileAlterationObserver observer) {
            System.out.println("onStop");
        }
    }
  • 相关阅读:
    java线程的几种状态
    java事务的处理
    Java多线程中Sleep与Wait的区别
    分享一百多套开发视频教程的下载地址
    [Java]读取文件方法大全
    Android开发人员必备的10 个开发工具
    CentOS 安装MySQL rpm方式安装
    记录一些经典的算法
    CentOS 7安装Redis服务
    linux查看文件大小,磁盘占用情况 du df命令
  • 原文地址:https://www.cnblogs.com/junge8618/p/8443421.html
Copyright © 2011-2022 走看看