Use Apache common IO to monitor file changes — to

<span style="color: #0000ff;">import<span style="color: #000000;"> org.apache.commons.io.filefilter.FileFilterUtils;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.apache.commons.io.monitor.FileAlterationListener;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.apache.commons.io.monitor.FileAlterationMonitor;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.apache.commons.io.monitor.FileAlterationObserver;

<span style="color: #0000ff;">import<span style="color: #000000;"> java.io.File;
<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.concurrent.TimeUnit;

<span style="color: #008000;">/**<span style="color: #008000;">

  • Created with IntelliJ IDEA.

  • User: superman

  • Date: 14-1-5

  • Time: 上午11:43

  • To change this template use File | Settings | File Templates.
    <span style="color: #008000;">*/
    <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> Example3 {
    <span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void main(String[] args) <span style="color: #0000ff;">throws<span style="color: #000000;"> Exception{
    File directory = <span style="color: #0000ff;">new File("D:/test"<span style="color: #000000;">);
    <span style="color: #008000;">//<span style="color: #008000;"> 轮询间隔 5 秒
    <span style="color: #0000ff;">long interval = TimeUnit.SECONDS.toMillis(5<span style="color: #000000;">);
    <span style="color: #008000;">//<span style="color: #008000;"> 创建一个文件观察器用于处理文件的格式
    FileAlterationObserver observer = <span style="color: #0000ff;">new<span style="color: #000000;"> FileAlterationObserver(directory,FileFilterUtils.and(
    FileFilterUtils.fileFileFilter(),FileFilterUtils.suffixFileFilter(".txt"<span style="color: #000000;">)));
    <span style="color: #008000;">//<span style="color: #008000;">设置文件变化监听器
    observer.addListener(<span style="color: #0000ff;">new<span style="color: #000000;"> MyFileListener());
    FileAlterationMonitor monitor = <span style="color: #0000ff;">new<span style="color: #000000;"> FileAlterationMonitor(interval,observer);
    monitor.start();
    <span style="color: #008000;">//<span style="color: #008000;">Thread.sleep(30000);
    <span style="color: #008000;">//<span style="color: #008000;">monitor.stop();
    <span style="color: #000000;"> }
    }
    <span style="color: #0000ff;">final <span style="color: #0000ff;">class MyFileListener <span style="color: #0000ff;">implements<span style="color: #000000;"> FileAlterationListener{
    @Override
    <span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onStart(FileAlterationObserver fileAlterationObserver) {
    System.out.println("monitor start scan files.."<span style="color: #000000;">);
    }

    @Override
    <span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onDirectoryCreate(File file) {
    System.out.println(file.getName()+" director created."<span style="color: #000000;">);
    }

    @Override
    <span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onDirectoryChange(File file) {
    System.out.println(file.getName()+" director changed."<span style="color: #000000;">);
    }

    @Override
    <span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onDirectoryDelete(File file) {
    System.out.println(file.getName()+" director deleted."<span style="color: #000000;">);
    }

    @Override
    <span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onFileCreate(File file) {
    System.out.println(file.getName()+" created."<span style="color: #000000;">);
    }

    @Override
    <span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onFileChange(File file) {
    System.out.println(file.getName()+" changed."<span style="color: #000000;">);
    }

    @Override
    <span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onFileDelete(File file) {
    System.out.println(file.getName()+" deleted."<span style="color: #000000;">);
    }

    @Override
    <span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onStop(FileAlterationObserver fileAlterationObserver) {
    System.out.println("monitor stop scanning.."<span style="color: #000000;">);
    }
    }

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>