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 27 28 29 30 31 32
|
Path path = Paths.get("path"); if (Files.exists(path)) try (WatchService watchService = FileSystems.getDefault().newWatchService()) path.register( watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE ); while (true) WatchKey key = watchService.take(); for (WatchEvent<?> event : key.pollEvents()) WatchEvent<Path> ev = (WatchEvent<Path>) event; Path file = ev.context();
if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind()))
if (StandardWatchEventKinds.ENTRY_MODIFY.equals(event.kind()))
if (StandardWatchEventKinds.ENTRY_DELETE.equals(event.kind())) key.reset(); catch (IOException | InterruptedException e) e.printStackTrace();
|