【问题标题】:Creating in java String Array contain paths when given a root folder给定根文件夹时,在java字符串数组中创建包含路径
【发布时间】:2012-11-11 18:05:23
【问题描述】:

我的计算机中有一个文件夹(一些根文件夹),其中包含许多文件夹和文件。我需要创建一个字符串数组,其中包含文件的所有路径(从根文件夹开始)(我的意思是只有叶子 = 文件,而不是文件夹)。我该怎么做?

【问题讨论】:

标签: java arrays string path root


【解决方案1】:

使用标准 Java SE 类和递归,您可以这样做:

import java.io.File;

public class Test {
    public static void main(String[] args) {
        File root = new File("D:\\Downloaded"); // path to root folder
        process(root);
    }

    private static void process(File path) {
        File[] subs = path.listFiles();
        if (subs != null) {
            for (File f : subs) {
                if (f.isDirectory()) {
                    process(f);
                } else {
                    System.out.println(f.getAbsolutePath());
                }
            }
        }
    }
}

请注意,您可能希望将路径放入ArrayList,而不是System.out.println()

【讨论】:

  • 那么,请教我如何将它放入 ArrayList 中?
猜你喜欢
  • 2011-05-01
  • 2018-03-08
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
  • 2018-07-27
  • 2019-09-25
  • 2020-07-04
相关资源
最近更新 更多