无法在Java中编译walkFileTree方法

时间:2014-07-19 06:23:27

标签: java file-io filevisitor

这是我的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方法。我正在尝试从here运行示例。我也看过使用上面代码的示例但不要'我知道为什么我面临问题。

0 个答案:

没有答案