apache commons-io相关介绍-monitor包

apache commons-io相关介绍-monitor包

org.apache.commons.io.monitor 文件状态监控

org.apache.commons.io.monitor包的作用监控指定目录下的文件状态。它使用观察者设计模式设计这些类的关系。当文件创建,文件修改和文件删除事件发生时会调用相应的回调函数。

基本使用方法:

package com.demo.monitor;
import java.io.File;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
public class Demo1 {
public static void main(String[] args) throws Exception {
    File directory = new File(new File("."), "src");
    FileAlterationObserver observer = new FileAlterationObserver(directory);
    observer.addListener(new MyFileAlterationListener());
    long interval = 1000;
    FileAlterationMonitor monitor = new FileAlterationMonitor(interval);
    monitor.addObserver(observer);
    monitor.start();
}

public static class MyFileAlterationListener implements
        FileAlterationListener {
    public void onStart(FileAlterationObserver observer) {
        // TODO Auto-generated method stub
        System.out.println("--------onStart ---------");
    }
    public void onDirectoryCreate(File directory) {
        // TODO Auto-generated method stub
        System.out.println("--------onDirectoryCreate ---------");
    }

    public void onDirectoryChange(File directory) {
        // TODO Auto-generated method stub
        System.out.println("--------onDirectoryChange ---------");
    }

    public void onDirectoryDelete(File directory) {
        // TODO Auto-generated method stub
        System.out.println("--------onDirectoryDelete ---------");
    }

    public void onFileCreate(File file) {
        // TODO Auto-generated method stub
        System.out.println("--------onFileCreate ---------");
    }

    public void onFileChange(File file) {
        // TODO Auto-generated method stub
        System.out.println("--------onFileChange ---------");
    }

    public void onFileDelete(File file) {
        // TODO Auto-generated method stub
        System.out.println("--------onFileDelete ---------");
    }

    public void onStop(FileAlterationObserver observer) {
        // TODO Auto-generated method stub
        System.out.println("--------onStop ---------");
    }

}

}

monitor.start();开启监控器,每隔interval毫秒,onStart会被执行一次,直接结束时,onStop将会被调用。Listener的创建过程以上代码是直接继承了FileAlterationListener接口,所有的接口函数必须被实现。这个过程比较繁琐。当调用者只关心其中一些事件却不得不实现所有函数。简化的方法是使用程序提供的一个适配器类FileAlterationListenerAdaptor(这里使用了适配器模式)。这样调用者只需重写关注的事件函数,大大简化了代码。代码如下:

package com.demo.monitor;
import java.io.File;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

public class Demo2 {
    public static void main(String[] args) throws Exception {
        File directory = new File(new File("."), "src");
        FileAlterationObserver observer = new FileAlterationObserver(directory);
        observer.addListener(new FileAlterationListenerAdaptor(){
            @Override
            public void onFileCreate(File file) {
                System.out.println("---------onFileCreate--------");
            }
        });
        long interval = 1000;
        FileAlterationMonitor monitor = new FileAlterationMonitor(interval);
        monitor.addObserver(observer);
        monitor.start();
    }
}
posted @ 2015-06-05 20:50  xiaozu  阅读(933)  评论(0编辑  收藏  举报