0

我有一个 java-project 的结构

folder -
        --sub_folder1
          ---file1
          ---file2  
        --sub_folder2
          ---file3
          ---file4

如何使用代码读取此结构?

4

2 回答 2

1

它来了 :)。

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());
        }
    }
}
于 2013-10-18T06:21:42.500 回答
0

这应该有效:

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; 
}
于 2013-10-18T06:23:01.393 回答