| 8 | |
| 9 | public class TestWatch { |
| 10 | public static void main(String [] args) throws Exception { |
| 11 | var watchService = FileSystems.getDefault().newWatchService(); |
| 12 | var directory = Paths.get("."); |
| 13 | directory.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); |
| 14 | while (true) { |
| 15 | var key = watchService.take(); |
| 16 | for (var event : key.pollEvents()) { |
| 17 | var kind = event.kind(); |
| 18 | if (kind == StandardWatchEventKinds.ENTRY_CREATE) { |
| 19 | @SuppressWarnings("unchecked") WatchEvent<Path> renameEvent = (WatchEvent<Path>) event; |
| 20 | Path renamedFile = renameEvent.context(); |
| 21 | System.out.println("Created: " + renamedFile); |
| 22 | } |
| 23 | } |
| 24 | if (!key.reset()) |
| 25 | break; |
| 26 | } |
| 27 | } |
| 28 | } |