这是我的 Java 类:
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);
}
}
这一行:
Files.walkFileTree(startingDir, pf);
抛出错误:PrintFiles cannot be converted to <? super Path>
这是可以接受的,但是当我将上面的行更改为:
Files.walkFileTree(startingDir, new java.nio.file.SimpleFileVisitor<Path> ());
它仍然显示错误SimpleFileVisitor() has protected access in SimpleFileVisitor.
所以我想如何运行 walkFileTree 方法。我正在尝试从这里运行示例。我也使用上面的代码看到了关于 SO 的示例,但不知道为什么我会遇到问题。