【发布时间】:2020-09-22 15:34:33
【问题描述】:
我只是在研究主题 nio 并得到了以下任务: 使用 nio 对文件名进行递归搜索。该方法应返回找到的路径列表。 当我在输出中运行下面的代码时,我只看到 [ ]。有人可以解释并纠正我吗?
public class Task01 {
public static void main(String[] args) throws IOException {
Path dir = Paths.get("C:\\Users\\......");
System.out.println(findFile(dir, "Task01.java"));
}
public static ArrayList<Path> findFile(Path path, String filename) throws IOException {
Path dir = Paths.get("C:\\....");
ArrayList<Path> list1 = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, filename)) {
for (Path entry : stream) {
if (path.toFile().isDirectory()) {
findFile(path, filename);
} else list1.add(entry.toAbsolutePath());
}
}
return list1;
}
}
【问题讨论】:
-
在您的
if..else语句中,使用entry而不是path