java 监听文件或者文件夹变化的几种方式

java 监听文件或者文件夹变化的几种方式

1.log4j的实现的文件内容变化监听

复制代码
package com.jp.filemonitor;
import org.apache.log4j.helpers.FileWatchdog;
public class Log4jWatchdog {
    public static void main(String[] args) {
        GloablConfig gloablConfig = new GloablConfig("D:\\create_lo\\text.txt");
        gloablConfig.setDelay(10000);
        gloablConfig.start();
        while (true) {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">class</span> GloablConfig <span style="color: #0000ff;">extends</span><span style="color: #000000;"> FileWatchdog{
    </span><span style="color: #0000ff;">protected</span><span style="color: #000000;"> GloablConfig(String filename) {
        </span><span style="color: #0000ff;">super</span><span style="color: #000000;">(filename);
    }

    @Override
    </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> doOnChange() {
        
        System.out.println(filename);
    }
    
}

}

复制代码

2.common-io实现的文件夹变化的监听

这代码网上很多,可以搜索关键字FileAlterationMonitor,FileAlterationObserver  FileAlterationObserver,这样就可以看到示例代码了。

3.nio实现文件夹内容变化的监听

复制代码
package com.jp.filemonitor;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ResourceListener {
private static ExecutorService fixedThreadPool = Executors.newCachedThreadPool();
private WatchService ws;
private String listenerPath;
private ResourceListener(String path) {
try {
ws = FileSystems.getDefault().newWatchService();
this.listenerPath = path;
start();
} catch (IOException e) {
e.printStackTrace();
}
}

</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> start() {
    fixedThreadPool.execute(</span><span style="color: #0000ff;">new</span> Listner(ws,<span style="color: #0000ff;">this</span><span style="color: #000000;">.listenerPath));
}

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> addListener(String path) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> IOException {
    ResourceListener resourceListener </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> ResourceListener(path);
    Path p </span>=<span style="color: #000000;"> Paths.get(path);
    p.register(resourceListener.ws,StandardWatchEventKinds.ENTRY_MODIFY,
        StandardWatchEventKinds.ENTRY_DELETE,
        StandardWatchEventKinds.ENTRY_CREATE);
}


</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> main(String[] args) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> IOException {
    ResourceListener.addListener(</span>"F:\\"<span style="color: #000000;">);
    ResourceListener.addListener(</span>"d:\\"<span style="color: #000000;">);
}

}

class Listner implements Runnable {
private WatchService service;
private String rootPath;

</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Listner(WatchService service,String rootPath) {
    </span><span style="color: #0000ff;">this</span>.service =<span style="color: #000000;"> service;
    </span><span style="color: #0000ff;">this</span>.rootPath =<span style="color: #000000;"> rootPath;
}

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> run() {
    </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
        </span><span style="color: #0000ff;">while</span>(<span style="color: #0000ff;">true</span><span style="color: #000000;">){
            WatchKey watchKey </span>=<span style="color: #000000;"> service.take();
            List</span>&lt;WatchEvent&lt;?&gt;&gt; watchEvents =<span style="color: #000000;"> watchKey.pollEvents();
            </span><span style="color: #0000ff;">for</span>(WatchEvent&lt;?&gt;<span style="color: #000000;"> event : watchEvents){
            
                </span><span style="color: #008000;">//</span><span style="color: #008000;">TODO 根据事件类型采取不同的操作。。。。。。。</span>
                System.out.println("["+rootPath+event.context()+"]文件发生了["+event.kind()+"]事件"+<span style="color: #000000;">    event.count());  
            }
            watchKey.reset();
        }
    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (InterruptedException e) {
        e.printStackTrace();
    }</span><span style="color: #0000ff;">finally</span><span style="color: #000000;">{
        System.out.println(</span>"fdsfsdf"<span style="color: #000000;">);
        </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
            service.close();
        } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
            e.printStackTrace();
        }
    }
    
}

}

复制代码

 

0
0
« 上一篇: 2016年国内开源maven镜像站点汇总
» 下一篇: mybatis中GenericTokenParser的用法

posted on 2016-09-02 09:18 xunianchong 阅读(5562) 评论(0) 编辑 收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值