【发布时间】:2015-11-22 18:25:20
【问题描述】:
我需要做的就是扫描一个目录(可能包含子目录)并获取其中包含的所有路径的列表。
编辑:更改整个问题以使其更具体!
【问题讨论】:
-
Files.walkFileTree
标签: java file arraylist directory
我需要做的就是扫描一个目录(可能包含子目录)并获取其中包含的所有路径的列表。
编辑:更改整个问题以使其更具体!
【问题讨论】:
Files.walkFileTree
标签: java file arraylist directory
我的尝试是:
try {
List<Path> pathsInDir = new ArrayList<>();
// Dir to scan
DirectoryStream<Path> newDirectoryStream = Files.newDirectoryStream(Paths.get("."));
for(Path path : newDirectoryStream) {
if(Files.isDirectory(path)) {
pathsInDir.add(path);
// recursive call to scan sub dir
} else {
// handle files.
}
}
} catch (IOException e) {
// TODO please handle the exception.
e.printStackTrace();
}
您可以使用java.nio.file.Path的方法来比较目录名。
【讨论】:
File class anymore。它已过时且信息不足。请改用Files 类。