使用WatchService监控文件夹

通过java7提供的WatchService API 实现对文件夹的监控

package service;

import config.Config;
import java.io.IOException;
import java.nio.file.*;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class WatchDirService {
    private WatchService watchService;
    private boolean notDone = true;

    public WatchDirService(String dirPath){
        init(dirPath);
    }

    private void init(String dirPath) {
        Path path = Paths.get(dirPath);
        try {
            watchService = FileSystems.getDefault().newWatchService();  //创建watchService
            path.register(watchService, 
            StandardWatchEventKinds.ENTRY_CREATE,
            StandardWatchEventKinds.ENTRY_MODIFY,
            StandardWatchEventKinds.ENTRY_DELETE); //注册需要监控的事件,ENTRY_CREATE 文件创建,ENTRY_MODIFY 文件修改,ENTRY_MODIFY 文件删除
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        System.out.print("watch...");
        while (notDone){
            try {
                WatchKey watchKey = watchService.poll(Config.POLL_TIME_OUT, TimeUnit.SECONDS); 
                if(watchKey != null){
                    List<WatchEvent<?>> events = watchKey.pollEvents();  //获取所有得事件
                    for (WatchEvent event : events){
                        WatchEvent.Kind<?> kind = event.kind(); 
                        if (kind == StandardWatchEventKinds.OVERFLOW){
                            //当前磁盘不可用
                            continue;
                        }
                        WatchEvent<Path> ev = event;
                        Path path = ev.context();
                        if(kind == StandardWatchEventKinds.ENTRY_CREATE){
                            System.out.println("create " + path.getFileName());
                        }else if(kind == StandardWatchEventKinds.ENTRY_MODIFY){
                            System.out.println("modify " + path.getFileName());
                        }else if(kind == StandardWatchEventKinds.ENTRY_DELETE){
                            System.out.println("delete " + path.getFileName());
                        }
                    }
                    if(!watchKey.reset()){ 
                        //已经关闭了进程
                        System.out.println("exit watch server");
                        break;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                return;
            }
        }
    }
}

就是这么简单就可以对一个文件夹进行监控了,完整带码地址:https://github.com/aii1991/WatchServerDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 太长了,还是转载吧...今天在看博客的时候,无意中发现了@Trinea在GitHub上的一个项目Android开源...
    庞哈哈哈12138阅读 20,323评论 3 283
  • 每天都能被了解到关于“萨德”事件, 第一为乐天等韩国的企业或者驻韩国的中国企业感到可惜,一边是政府一边是自己奋斗成...
    少根筋的张先森阅读 2,515评论 0 0
  • 第一次有那种喜欢一个人,喜欢到骨子里的感觉,控制不住的思念,这往往就是所谓的心动吧,你的一举一动我都想关注,我知道...
    言许阅读 1,501评论 0 0
  • 4月22日愿望:顺利上好公开课 结果:孩子们喜欢、家长们满意,领导同事们信任和支持,自我肯定和认可,能力和自信心得...
    潮女搭配心经阅读 1,540评论 0 0