特定のディレクトリとそのすべてのサブディレクトリの変更 (特にファイルとディレクトリの追加) を監視する必要があります。
私の現在のコードは次のとおりです
Path watchFolder = Paths.get("D:/watch");
WatchService watchService = FileSystems.getDefault().newWatchService();
watchFolder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
watchFolder.register(watchService, StandardWatchEventKinds.ENTRY_DELETE);
watchFolder.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
boolean valid = true;
do {
WatchKey watchKey = watchService.take();
for (WatchEvent event : watchKey.pollEvents()) {
WatchEvent.Kind kind = event.kind();
if (StandardWatchEventKinds.ENTRY_CREATE.equals(kind)) {
String fileName = event.context().toString();
System.out.println("File Created:" + fileName);
}
}
valid = watchKey.reset();
} while (valid);
このコードは、親ディレクトリの変更のみを監視できます。親ディレクトリにディレクトリを追加すると、イベントを発生させることができます。しかし、サブディレクトリ内にファイルを追加すると、検出できません。
参考:私もJNotifyを試しましたが、それは言い続けますjava.lang.UnsatisfiedLinkError: no jnotify_64bit in java.library.path
これらよりも良い解決策はありますか?