I am using watchKey
to listen for a file change in a particular folder.
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();
}
}
It working fine for me. If I modify a file in raja folder it gives me the file name with path. But, when I put some files in subfolders like "E:/Raja/Test", it gives me only the path where I put it, not the file name.
How to get the file name?