JDK7目录监视服务
2018-07-20 来源:open-open
import java.io.IOException;
import java.nio.file.*;
public class DirectoryWatcher {
public static void main(String[] args) throws IOException, InterruptedException {
WatchService watchService = FileSystems.getDefault().newWatchService();
Path path = Paths.get("F:", "share").toAbsolutePath();
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.ENTRY_DELETE);
while(true) {
WatchKey watchKey = watchService.take();
for (WatchEvent event : watchKey.pollEvents()) {
if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
System.out.println("Create " + path.resolve((Path) event.context()).toAbsolutePath());
} else if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
System.out.println("Modify " + path.resolve((Path) event.context()).toAbsolutePath());
} else {
System.out.println("Delete " + path.resolve((Path) event.context()).toAbsolutePath());
}
}
watchKey.reset();
// Cancel the watch
// watchKey.cancel();
}
}
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐