1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| String path = ""; WatchService service = FileSystems.getDefault().newWatchService(); Path p = Paths.get(path); p.register( service, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_CREATE);
while (true) { WatchKey watchKey = service.take(); List<WatchEvent<?>> watchEvents = watchKey.pollEvents(); for (WatchEvent<?> event : watchEvents) { WatchEvent.Kind<?> eventKind = event.kind(); System.out.println( String.format( "文件夹 [%s] 中文件 [%s] 进行了 [%s] 事件, 重复[%s]次.", path, event.context(), eventKind, event.count())); System.out.println("file=" + event.context().toString()); } watchKey.reset(); Thread.sleep(10000); }
|