我怎么能读取java项目的文件结构?

时间:2013-10-18 06:16:19

标签: java file structure

我有一个java-project结构

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

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

2 个答案:

答案 0 :(得分: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());
        }
    }
}

答案 1 :(得分: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; 
}