Java: nio WatchService - не удается обнаружить создание второго файла.Зачем? - PullRequest
2 голосов
/ 14 сентября 2011

Я пытаюсь обнаружить создание файла в папке, но WatchService не может обнаружить создание второго файла.он застрял на key = watcher.take().Есть идеи почему?Я использую Ubuntu 11.04 x86.

private void watchWorkingDirectory() {
    try {
        WatchService watcher = FileSystems.getDefault().newWatchService();
        WatchKey key;
        key = Paths.get(tailSource, "").register(watcher, ENTRY_CREATE);

        for (;;) {

            System.err.println("Watching current working directory...............................................");
            // wait for key to be signalled
            key = watcher.take();

            System.out.println("Event detected:");


            for (WatchEvent<?> event : key.pollEvents()) {
                WatchEvent.Kind<?> kind = event.kind();

                if (kind == ENTRY_CREATE) {
                    tailSource = event.context().toString();
                    System.out.println(tailSource);
                    File file = new File(tailSource);
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    System.out.println(br.readLine());
                    tailer = new Tailer(file, tListener, 1, false);
                    (new Thread(tailer)).start();
                }
            }
        }

    } catch (IOException | InterruptedException e) {
        System.out.println(e.getMessage());
    }
}

}

1 Ответ

3 голосов
/ 14 сентября 2011

Вы должны вызывать key.reset () после обработки событий.

http://blogs.oracle.com/thejavatutorials/entry/watching_a_directory_for_changes

...