两种方法:
1.FIles.walkFileTree()函数直接遍历此处有详细说明
2.新建DirectoryStream<Path>对象遍历当前路径,遇到文件夹再递归遍历
public void walkFileTreeByStream(Path dir) {
try(DirectoryStream<Path> stream = Files.newDirectoryStream(dir)){
for(Path path : stream){
if( Files.isDirectory() ) {
this.walkFileTreeByStream(path);
}else{
System.out.println(path.getFileName());
}
}
System.out.println(dir.getFileName());
}catch(IOException e){
}
}
网友评论