[英]WatchService watch specfic directory for creation of files
public static void main (String args[]) throws Exception {
Path _directotyToWatch = Paths.get(args[0]);
WatchService watcherSvc = FileSystems.getDefault().newWatchService();
WatchKey watchKey = _directotyToWatch.register(watcherSvc, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
while (true) {
watchKey=watcherSvc.take();
for (WatchEvent<?> event: watchKey.pollEvents()) {
WatchEvent<Path> watchEvent = castEvent(event);
System.out.println(event.kind().name().toString() + " " + _directotyToWatch.resolve(watchEvent.context()));
watchKey.reset();
}
}
}
在上面的示例中,監視目錄路徑來自控制台參數。 我想靜態傳遞目錄路徑。
嘗試過此Paths.get(“ O:\\\\ test”); 但是拋出異常
Exception in thread "main" java.lang.NoClassDefFoundError: java/nio/file/Paths
at JSR203_NIO2_WatchFolder.main(JSR203_NIO2_WatchFolder.java:40)
Caused by: java.lang.ClassNotFoundException: java.nio.file.Paths
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
我只是遇到了這個問題,我想你想要的是:
Path path = FileSystems.getDefault().getPath(path_string);
嘗試
Path _directotyToWatch = Paths.get("O:/test");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.