I played around a little bit with your code and added a couple of lines:
/*
* (non-Javadoc)
*
* @see java.nio.file.SimpleFileVisitor#preVisitDirectory(java.lang.Object,
* java.nio.file.attribute.BasicFileAttributes)
*/
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
System.out.println(Thread.currentThread().getStackTrace()[1] + " "
+ dir);
return super.preVisitDirectory(dir, attrs);
}
/*
* (non-Javadoc)
*
* @see java.nio.file.SimpleFileVisitor#postVisitDirectory(java.lang.Object,
* java.io.IOException)
*/
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
System.out.println(Thread.currentThread().getStackTrace()[1] + " "
+ dir);
return super.postVisitDirectory(dir, exc);
}
/*
* (non-Javadoc)
*
* @see java.nio.file.SimpleFileVisitor#visitFileFailed(java.lang.Object,
* java.io.IOException)
*/
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc)
throws IOException {
System.out.println(Thread.currentThread().getStackTrace()[1] + " "
+ file);
return super.visitFileFailed(file, exc);
}
just no-brainers to see what's going on.
What you call a bug happens here java.nio.file.FileTreeWalker:134
:
// at maximum depth or file is not a directory
if (depth >= maxDepth || !attrs.isDirectory()) {
return visitor.visitFile(file, attrs);
}
Finally, what I think about it:
1.: subdir is part of depth 1
2.: Without the visitFile
for deepest directories, you wouldn't even recognize they are there.
So I think that's regular behaviour.
By the way, you could use the attrs in your override of:
@Override
public FileVisitResult visitFile(final Path file,
final BasicFileAttributes attrs) throws IOException {
if (attrs.isDirectory()) {
throw new IllegalStateException("WAT!? Visiting directory: "
+ file.toAbsolutePath().toString());
}
System.out
.println("Visiting file: " + file.toAbsolutePath().toString());
return super.visitFile(file, attrs);
}