问题
I'm recursively watching a directory (and therefore all subdirs and files) for changes.
It seems, if I'm creating or deleting a directory or file in a subdirectory of the root-dir to watch the Path which is included in the WatchEvent instance one receives (via context()) has no Parent and therefore rootDirToWatch.resolve(event.context())
is not returning the Path I like to have.
For instance:
/home/johannes/test
is watched, then I'm creating a new directory in /home/johannes/test/foo/bar
named baz
, I'm getting a new Path instance which is
/home/johannes/test/baz
instead of /home/johannes/test/foo/bar/baz
Any suggestions what's going wrong?
I'm simply using a visitor to watch for all subdirectories in a certain root-directory to watch (watching a whole directory with all it's descendants):
@Override
public FileVisitResult preVisitDirectory(final Path pDir, final BasicFileAttributes pAttrs)
throws IOException
{
checkNotNull(pDir);
checkNotNull(pAttrs);
pDir.register(mWatcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
return FileVisitResult.CONTINUE;
}
Edit: I think I really have to use a visitor or at least register all subdirs with the watcher. As WatchEvent returns a relative path it's clear why it behaves as described, but I don't want to traverse the directory once more to find the path from the root-dir to watch to the added/deleted/modified File somewhere depper in the hierarchy.
Edit: I've found the solution ("indexing" the keys): http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java
回答1:
Alan is right.
You should use the Watchable in the key.
Here's a Scala code from my app. I guess you will be able to read it.
object FsNotifType extends Enumeration {
val Create, Update, Delete = Value
}
case class FsNotif(notifType: FsNotifType.Value,path: Path)
object FsNotif {
def apply(watchKey: WatchKey,event: java.nio.file.WatchEvent[_]): FsNotif = {
val fsNotifType = event.kind() match {
case StandardWatchEventKinds.ENTRY_CREATE => FsNotifType.Create
case StandardWatchEventKinds.ENTRY_MODIFY => FsNotifType.Update
case StandardWatchEventKinds.ENTRY_DELETE => FsNotifType.Delete
case _ => throw new IllegalStateException("Unknown FS event kind: " + event)
}
val watchedPath = watchKey.watchable().asInstanceOf[Path]
val relativePath = event.context().asInstanceOf[Path]
val absolutePath = watchedPath.resolve(relativePath)
FsNotif(fsNotifType,absolutePath)
}
}
This works fine, but take care of some corner cases:
/**
* Returns the object for which this watch key was created. This method will
* continue to return the object even after the key is cancelled.
*
* <p> As the {@code WatchService} is intended to map directly on to the
* native file event notification facility (where available) then many of
* details on how registered objects are watched is highly implementation
* specific. When watching a directory for changes for example, and the
* directory is moved or renamed in the file system, there is no guarantee
* that the watch key will be cancelled and so the object returned by this
* method may no longer be a valid path to the directory.
*
* @return the object for which this watch key was created
*/
Watchable watchable();
Sorry, I don't know how to deal with this.
回答2:
The watchable() method will return the original Watchable so you can use it as the parent directory.
来源:https://stackoverflow.com/questions/8205842/watchservice-watchevent-path-from-event