java 7 监控文件状态(修改,删除,增加)

package com.filewatch;

import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE; import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; import static java.nio.file.StandardWatchEventKinds.OVERFLOW;

import java.io.File; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.WatchEvent; import java.nio.file.WatchKey; import java.nio.file.WatchService; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import java.util.concurrent.ConcurrentHashMap;   public class FileWatch {      Map<WatchKey,Path> keys = new ConcurrentHashMap<WatchKey,Path>();      private static WatchService watcher = null;            static {          try {              watcher = FileSystems.getDefault().newWatchService();  //构建文件监控服务         } catch (IOException e) {              e.printStackTrace();          }         }               private void register(Path dir) throws IOException { // IOException ,InterruptedException{          WatchKey key = dir.register(watcher, ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY);   //给文件注册监听事件             Path existing = keys.get(key);            if (existing == null) {                System.out.format("register: %s\n", dir);            } else if (!dir.equals(existing)){                 System.out.format("update: %s -> %s\n",existing, dir);            }                       keys.put(key, dir);       }            @SuppressWarnings("unchecked")       static <T> WatchEvent<Path> cast(WatchEvent<?> event) {           return (WatchEvent<Path>)event;       }       private void registPath(File files) throws IOException{       for (File file:files.listFiles()) {     if(file.isDirectory()){       register(Paths.get(file.getPath()));       registPath(file);     }    }     }         private void fileWatch(Path dir) throws IOException,InterruptedException{               register(dir);//先注册主文件夹         registPath(dir.toFile());//再通过递归方式将子文件夹也注册进去         while(true){          // 等待监视事件发生           WatchKey key = watcher.take();                   // System.out.println(key.getClass().getName());           Path path = keys.get(key);           if (path == null) {               return;           }                    for (WatchEvent<?> event : key.pollEvents()) {               WatchEvent.Kind kind = event.kind();               if (kind == OVERFLOW) {                   continue;               }                              // 目录监视事件的上下文是文件名               WatchEvent<Path> evt = cast(event);               Path name = evt.context();               Path child = path.resolve(name);              System.out.format(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + "  %s|%s\n", event.kind().name(), child);               if(child.toFile().isDirectory() && event.kind().name().equals("ENTRY_CREATE")){//对于新增的文件夹以及其子文件夹也要加入监控               register(child);               registPath(child.toFile());              }          }              // 重置 key           boolean valid = key.reset();           if (!valid) {               keys.remove(key);               if (keys.isEmpty()) {                   return;               }           }          }     }                public static void main(String[] args) {       FileWatch fileWatch = new FileWatch();                    Path dir = Paths.get("D:\\test2\\");          try {           fileWatch.fileWatch(dir);          } catch (IOException | InterruptedException e) {              e.printStackTrace();          }      }  } 

转载于:https://my.oschina.net/u/615618/blog/174736

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值