Reputation: 892
This is my Java Class:
public class PrintFiles extends java.nio.file.SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attr) {
if (attr.isSymbolicLink()) {
System.out.format("Symbolic link: %s ", file);
} else if (attr.isRegularFile()) {
System.out.format("Regular file: %s ", file);
} else {
System.out.format("Other: %s ", file);
}
System.out.println("(" + attr.size() + "bytes)");
return CONTINUE;
}
// Print each directory visited.
@Override
public FileVisitResult postVisitDirectory(Path dir,
IOException exc) {
System.out.format("Directory: %s%n", dir);
return CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file,
IOException exc) {
System.err.println(exc);
return CONTINUE;
}
public static void main(String args[])
{
java.nio.file.Path startingDir = Paths.get("D:\\");
PrintFiles pf = new PrintFiles();
Files.walkFileTree(startingDir, pf);
}
}
This line:
Files.walkFileTree(startingDir, pf);
throws error: PrintFiles cannot be converted to <? super Path>
which is acceptable but When I change the above line to:
Files.walkFileTree(startingDir, new java.nio.file.SimpleFileVisitor<Path> ());
it still shows the error SimpleFileVisitor() has protected access in SimpleFileVisitor.
So how am i suppose to run walkFileTree method.I am trying to run the example from here.I have also seen examples on SO using above code but don't know why I am facing problem.
Upvotes: 1
Views: 499