gpt4 book ai didi

Java:无法覆盖 postVisitDirectory

转载 作者:行者123 更新时间:2023-12-02 08:50:42 25 4
gpt4 key购买 nike

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

/**
* 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);
}
}

感谢您的帮助。

最佳答案

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

关于Java:无法覆盖 postVisitDirectory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60798316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com