I recursively scan the directory (and therefore all sub-folders and files) for changes.
It seems if I create or delete a directory or file in a subdirectory of the root directory to see the Path that is included in the WatchEvent instance that you receive (via context ()) does not have a parent, and therefore rootDirToWatch.resolve(event.context()) doesn't return the path that I like.
For instance:
/home/johannes/test , then I create a new directory in /home/johannes/test/foo/bar named baz , I get a new Path instance that is /home/johannes/test/baz instead of /home/johannes/test/foo/bar/baz
Any suggestions what goes wrong?
I just use the visitor to watch all the subdirectories in a specific root directory to watch (by looking at the whole folder with all its 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 need to use a visitor, or at least register all the sub-racers with an observer. Since WatchEvent returns a relative path, it clearly explains why it behaves as described, but I donβt want to navigate the directory again to find the path from the -dir root to look at the added / deleted / modified file somewhere below in hierarchy.
Edit: I found a solution ("indexing" the keys): http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/essential/io/ examples / WatchDir.java
source share