Sto utilizzando watchKey
per ascoltare una modifica di file in una cartella particolare.Monitorare le sottocartelle con un servizio di visualizzazione Java
Path _directotyToWatch = Paths.get("E:/Raja");
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();
}
}
farlo funzionare bene per me. Se modifico un file nella cartella raja mi dà il nome del file con percorso. Ma quando inserisco alcuni file in sottocartelle come "E:/Raja/Test", mi dà solo il percorso in cui l'ho inserito, non il nome del file.
Come ottenere il nome del file?
questo è un duplicato di http://stackoverflow.com/questions/5608234/how-can-i-watch-subdirectory-for-changes-with-watch-service-java –