WatchService 監聽一個文件夾下全部層的變化 (新添加的文件也能被監聽)

原由

       我在開發我的網站 搜網盤(www.souwp.cn) 的時候,須要實現這樣一個功能:網站會實時的生成一些新的網頁,我須要把這些新增的網頁提交給搜索引擎收錄。首先想到的是寫在生成新網頁的代碼裏面,可是感受這樣耦合性過高,不方便維護。因此想着有沒有別的替代方法,就想到了Java7的新特性WatchService 能夠實現對目錄的監聽,而後就能夠作一樣的事情了。java

        在實現了 上一步 後,這回接着解決子不能監聽子目錄下文件變化的問題。ide

Path targetPath = Paths.get("E:\\temp");
try(WatchService watchService = targetPath.getFileSystem().newWatchService()) {
            // 讓 targetPath 下的全部文件夾(多層)都註冊到 watchService
            Files.walkFileTree(targetPath, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    dir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
                            StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
                    return FileVisitResult.CONTINUE;
                }
            });

            WatchKey watchKey = null;
            while (true) {
                try {
                    watchKey = watchService.take();

                    List<WatchEvent<?>> watchEvents = watchKey.pollEvents();
                    for (final WatchEvent<?> event : watchEvents) {
                        WatchEvent<Path> watchEvent = (WatchEvent<Path>) event;
                        WatchEvent.Kind<Path> kind = watchEvent.kind();
                        Path watchable = ((Path) watchKey.watchable()).resolve(watchEvent.context());


                        // 在監聽到文件夾建立的時候要把這個 path 註冊到 watchService 上
                        if(Files.isDirectory(watchable)){
                            if(kind == StandardWatchEventKinds.ENTRY_CREATE) {
                                System.out.println("新建目錄 = " + watchable);
                                watchable.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
                                        StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
                            }

                        } else {
                            if(kind == StandardWatchEventKinds.ENTRY_CREATE) {
                                System.out.println("新建文件 = " + watchable);

                            } else if(StandardWatchEventKinds.ENTRY_MODIFY == event.kind()){
                                System.out.println("修改文件 = " + watchable);

                            } else if(StandardWatchEventKinds.ENTRY_DELETE == event.kind()){
                                System.out.println("刪除文件 = " + watchable);
                            }
                        }

                        /*if (!watchKey.reset()) {
                            break;
                        }*/
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if(watchKey != null){
                        watchKey.reset();
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        運行結果:網站

            

        本文參考 https://blog.csdn.net/qq_31871635/article/details/81164285,並作了適當修改, 感謝做者。搜索引擎

相關文章
相關標籤/搜索