欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用递归查找文件

程序员文章站 2022-03-02 23:12:32
...
package eee;

import java.util.*;
import java.io.*;

public class FileRecursionPrintDemo {
public static void main(String[] args) {
List<String> paths = new ArrayList<String>();
paths = getAllFilePaths(new File("C:\\Users\\Administrator\\Desktop\\bd_3.2.1_setup"), paths);
for (String path : paths) {
System.out.println(path);
}
}

private static List<String> getAllFilePaths(File filePath, List<String> filePaths) {
File[] files = filePath.listFiles();
if (files == null) {
return filePaths;
}
for (File f : files) {
if (f.isDirectory()) {
filePaths.add(f.getPath());
getAllFilePaths(f, filePaths);
} else {
filePaths.add(f.getPath());
}
}
return filePaths;
}
}
相关标签: c