Java 7 WatchService - Ignoring multiple occurrences of the same event

后端 未结 14 630
情歌与酒
情歌与酒 2020-12-05 02:21

The javadoc for StandardWatchEventKinds.ENTRY_MODIFY says:

Directory entry modified. When a directory is registered for this event the

14条回答
  •  青春惊慌失措
    2020-12-05 02:40

    I compiled Oracle's WatchDir.java and @nilesh's suggestion into an Observable class that will notify it's observers once when the watched file is changed.

    I tried to make it as readable and short as possible, but still landed with more than 100 lines. Improvements welcome, of course.

    Usage:

    FileChangeNotifier fileReloader = new FileChangeNotifier(File file);
    fileReloader.addObserver((Observable obj, Object arg) -> {
        System.out.println("File changed for the " + arg + " time.");
    });
    

    See my solution on GitHub: FileChangeNotifier.java.

提交回复
热议问题