【问题标题】:Java: Can't override postVisitDirectoryJava:无法覆盖 postVisitDirectory
【发布时间】:2020-03-22 10:41:17
【问题描述】:

我编写了一个单一用途的程序来遍历一个目录并将所有目录和文件名打印到一个文件中,但是编译器抛出一个错误,上面写着“dirWalkerPrinter\DirWalkerPrinter.java:52: error: method does not override或从超类型实现方法”。程序代码如下:

/**
 * This program walks a directory tree
 * and prints out the directory name and the file names under it.
 * @author Michael Mei
 * @version 1.0 22-03-2020
 */

package walkDirectory;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;
import static java.nio.file.FileVisitOption.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class DirWalkerPrinter extends SimpleFileVisitor<Path> {
    private Path outPath;
    private Writer out;
    private int fileCount;
    private int dirCount;

    DirWalkerPrinter (Path outPath) throws IOException {
        this.outPath = outPath;
        out = Files.newBufferedWriter(outPath, StandardCharsets.UTF_16, StandardOpenOption.WRITE);
    }

    public int getFileCount () {
        return fileCount;
    }

    public int getDirCount () {
        return dirCount;
    } 

    public void writeResults(Path p) throws IOException {
        out.write(p.toString());
        out.write("\n");
    }

    public void done () throws IOException {
        out.write(fileCount + " of files found in " + dirCount);
    }

    @Override
    public FileVisitResult visitFile (Path file, BasicFileAttributes attrs) throws IOException {
        writeResults(file.getFileName());
        fileCount++;
        return CONTINUE;
    }
    @Override  // Error message points to this line
    public FileVisitResult postVisitDirectory (Path dir, BasicFileAttributes attrs) throws IOException {
        writeResults(dir);
        dirCount++;
        return CONTINUE;
    }
    @Override
    public FileVisitResult visitFileFailed (Path file, IOException e) {
        System.err.println(e);
        return CONTINUE;
    }

    public static void main(String[] args) throws IOException {
        if (args.length < 2) {
        System.err.println("java DirWalkerPrinter source-path destination-file");
        System.exit(-1);
    }
        Path startingDir = Paths.get(args[0]);
        Path writeToDir = Paths.get(args[1]);
        DirWalkerPrinter dirWalkerPrinter = new DirWalkerPrinter(writeToDir);
        Files.walkFileTree(startingDir, dirWalkerPrinter);
        dirWalkerPrinter.done();
        int fileCount = dirWalkerPrinter.getFileCount();
        int dirCount = dirWalkerPrinter.getDirCount();
        System.out.println(fileCount + " of files found in " + dirCount);
    }
}

感谢您的帮助。

【问题讨论】:

    标签: java file io


    【解决方案1】:

    如果您想覆盖 @Override 注释,请从 postVisitDirectory 中删除它。使用相同的签名。

    【讨论】:

    • 问题是您不再覆盖该方法。查看文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多