A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© Lemen 中级黑马   /  2015-8-22 22:16  /  237 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

发个例子给大家看下吧
  1. public class Test {

  2.     public static void main(String[] args) {
  3.         WatchService watcher = null;
  4.         try {
  5.             watcher = FileSystems.getDefault().newWatchService();
  6.             Path dir = FileSystems.getDefault().getPath("E:\\");

  7.             WatchKey key = dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE,
  8.                     StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);

  9.             while (true) {
  10.                 key = watcher.take();
  11.                 for (WatchEvent<?> event : key.pollEvents()) {
  12.                     System.out.println(event.kind().name());
  13.                     System.out.println(event.context());
  14.                     System.out.println("00-------------------00");
  15.                 }
  16.                 key.reset();
  17.             }
  18.         } catch (IOException e) {
  19.             e.printStackTrace();
  20.         } catch (InterruptedException e) {
  21.             e.printStackTrace();
  22.         }


  23.     }
  24. }
复制代码

1 个回复

正序浏览
谢谢楼主分享!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马