Java 7 WatchService - Ignoring multiple occurrences of the same event

后端 未结 14 602
情歌与酒
情歌与酒 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:50

    I solved this problem by defining a global boolean variable named "modifySolver" which be false by default. You can handle this problem as I show bellow:

    else if (eventKind.equals (ENTRY_MODIFY))
            {
                if (event.count() == 2)
                {
                    getListener(getDirPath(key)).onChange (FileChangeType.MODIFY, file.toString ());
                }
                /*capture first modify event*/
                else if ((event.count() == 1) && (!modifySolver))
                {
                    getListener(getDirPath(key)).onChange (FileChangeType.MODIFY, file.toString ());
                    modifySolver = true;
                }
                /*discard the second modify event*/
                else if ((event.count() == 1) && (modifySolver))
                {
                    modifySolver = false;
                }
            }
    

提交回复
热议问题