The reason why you're not getting the file name created/modified inside a subfolder is given by Stephen C in his answer.
Here is a simple example of how to register directories and subdirectories to watch them for the events you are interested in:
/**
* Register the given directory and all its sub-directories with the WatchService.
*/
private void registerAll(final Path start) throws IOException {
// register directory and sub-directories
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
return FileVisitResult.CONTINUE;
}
});
}
Check out the official Java Tutorials: Watching a Directory for Changes. There you can find very nice explanations and examples with the source code.
Particularly you'll be interested in this example of how to watch a directory (or directory tree) for changes to files: WatchDir.java.
The method I supplied above was taken from this example (omitting some parts for brevity).
Read the tutorial for the details.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…