java 使用Java WatchService观察目录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 使用Java WatchService观察目录相关的知识,希望对你有一定的参考价值。

Path myDir = Paths.get(DIRECTORY_NAME);

WatchService watcher = myDir.getFileSystem().newWatchService();
myDir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE,
		StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);

WatchKey watchKey = watcher.take();

while (watchKey != null) {
	for (WatchEvent event : watchKey.pollEvents()) {
		if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
			log.info(((Path) event.context()).getFileName().toString());
		}
	}
	watchKey.reset();
	watchKey = watcher.take();
}

以上是关于java 使用Java WatchService观察目录的主要内容,如果未能解决你的问题,请参考以下文章