我有一个 java-project 的结构
folder -
--sub_folder1
---file1
---file2
--sub_folder2
---file3
---file4
如何使用代码读取此结构?
它来了 :)。
public static void main(String[] args) {
filesInFolder("./");
}
public static void filesInFolder(String filename) {
File dir = new File(filename);
for (File child : dir.listFiles()) {
System.out.println(child.getAbsolutePath());
if (child.isDirectory()){
filesInFolder(child.getAbsolutePath());
}
}
}
这应该有效:
String projectRoot = "path to your root folder";
List<File> projectFiles = getProjectFiles(new File(projectRoot));
private List<File> getProjectFiles(File root) {
List<File> projectFiles = new ArrayList<File>();
for (File f : root.listFiles()) {
if (f.isDirectory())
projectFiles.addAll(getProjectFiles(f));
else
projectFiles.add(f);
}
return projectFiles;
}