Java 7 NIO notification

Java 7 NIO notification

Igazából ide nincs milyen példát felhozni, hiszen csaknem ugyan az, mint a JPathwatch. Szinte csak az importokat kell lecserélni.

Jellemzője:

Rekurzív könyvtárvizsgálat

Ugyan alapbül nem támogatja, de azért természetesen megoldható. A WatchDir.java példában következő módon:

 1    /**
 2     * Register the given directory with the WatchService
 3     */
 4    private void register(Path dir) throws IOException {
 5        WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
 6        if (trace) {
 7            Path prev = keys.get(key);
 8            if (prev == null) {
 9                System.out.format("register: %s\n", dir);
10            } else {
11                if (!dir.equals(prev)) {
12                    System.out.format("update: %s -> %s\n", prev, dir);
13                }
14            }
15        }
16        keys.put(key, dir);
17    }
18
19    /**
20     * Register the given directory, and all its sub-directories, with the
21     * WatchService.
22     */
23    private void registerAll(final Path start) throws IOException {
24        // register directory and sub-directories
25        Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
26            @Override
27            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
28                throws IOException
29            {
30                register(dir);
31                return FileVisitResult.CONTINUE;
32            }
33        });
34    }

További kapcsolódó linkek:


Directory Watcher
Apr 20, 2012
comments powered by Disqus

Links

Cool

RSS