列出文件夹和文件

破百叶窗:

我用这段代码列出了所有文件夹...

    public static void main(String[] args) throws Exception {
    File root = new File("C:\\Users\\resti\\Desktop\\example");
    if (!root.isDirectory())
    {
        System.out.println("some_text");
    }

    int level = 0;
        System.out.println(renderFolder(root, level, new StringBuilder(), false, new ArrayList<>()));

        }
    private static StringBuilder renderFolder(File folder, int level, StringBuilder sb, boolean isLast, List<Boolean> hierarchyTree) {
    indent(sb, level, isLast, hierarchyTree).append(folder.getName()).append("\n");

    File[] objects = folder.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File current, String name) {
            return new File(current, name).isDirectory();
        }
    });

    for (int i = 0; i < objects.length; i++) {
        boolean last = ((i + 1) == objects.length);

        // this means if the current folder will still need to print subfolders at this level, if yes, then we need to continue print |
        hierarchyTree.add(i != objects.length - 1);
        renderFolder(objects[i], level + 1, sb, last, hierarchyTree);

        // pop the last value as we return from a lower level to a higher level
        hierarchyTree.remove(hierarchyTree.size() - 1);
    }
    return sb;
}


private static StringBuilder indent(StringBuilder sb, int level, boolean isLast, List<Boolean> hierarchyTree) {
    String indentContent = "\u2502   ";
    for (int i = 0; i < hierarchyTree.size() - 1; ++i) {
        // determines if we need to print | at this level to show the tree structure
        // i.e. if this folder has a sibling foler that is going to be printed later
        if (hierarchyTree.get(i)) {
            sb.append(indentContent);
        } else {
            sb.append("    "); // otherwise print empty space
        }
    }

    if (level > 0) {
        sb.append(isLast
                ? "\u2514\u2500\u2500"
                : "\u251c\u2500\u2500");
    }

    return sb;
}
}

它可以正常工作,但很酷,但问题是它不计算.txt或.png等普通文件...我知道它写在此方法中,但是idk如何解决它而不破坏其他所有内容

File[] objects = folder.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File current, String name) {
            return new File(current, name).isDirectory();
        }
    });

我可以重做该方法或其他想法吗?(此代码不是我的https://stackoverflow.com/a/33438475/13547682

自由党:

假设我了解您的问题,这就是我的答案:

在给定的代码中:

File[] objects = folder.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File current, String name) {
            return new File(current, name).isDirectory();
        }
);

您正在过滤在文件夹中找到的所有文件,并使用.isDirectory()检查给定文件是否为目录。因此,所得数组仅包含文件夹。

如果需要文件夹和文件,则只需删除过滤器,即可在给定文件夹中拥有所有文件/文件夹。

File[] objects = folder.listFiles();

因为代码现在还必须处理文件,所以我们必须更改一些处理缩进的代码。

在更改文件和文件夹之前,代码将对object数组中的每个元素以相同的方式起作用。但是现在我们要区分文件夹和文件。

从而:

private static StringBuilder renderFolder(File folder, int level, StringBuilder sb, boolean isLast, List<Boolean> hierarchyTree) {
    indent(sb, level, isLast, hierarchyTree).append(folder.getName()).append("\n");

    File[] objects = folder.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File current, String name) {
            return new File(current, name).isDirectory();
        }
    });

    for (int i = 0; i < objects.length; i++) {
        boolean last = ((i + 1) == objects.length);

        // this means if the current folder will still need to print subfolders at this level, if yes, then we need to continue print |
        hierarchyTree.add(i != objects.length - 1);
        renderFolder(objects[i], level + 1, sb, last, hierarchyTree);

        // pop the last value as we return from a lower level to a higher level
        hierarchyTree.remove(hierarchyTree.size() - 1);
    }
    return sb;
}

必须将其更改为如下所示:

private static StringBuilder renderFolder(File folder, int level, StringBuilder sb, boolean isLast, List<Boolean> hierarchyTree) {
        indent(sb, level, isLast, hierarchyTree).append(folder.getName()).append("\n");

        File[] objects = folder.listFiles();

        for (int i = 0; i < objects.length; i++) {
            boolean last = ((i + 1) == objects.length);
            hierarchyTree.add(i != objects.length - 1);
            if (objects[i].isDirectory() == false) {
                indent(sb, level, isLast, hierarchyTree).append(objects[i].getName()).append("\n");
                hierarchyTree.remove(hierarchyTree.size() - 1);
                continue;
            }
            // this means if the current folder will still need to print subfolders at this level, if yes, then we need to continue print |
            renderFolder(objects[i], level + 1, sb, last, hierarchyTree);

            // pop the last value as we return from a lower level to a higher level
            hierarchyTree.remove(hierarchyTree.size() - 1);
        }
        return sb;
}

重要的更改是,如果数组中的对象不是文件夹,则将调用indent函数,然后继续循环,而无需在当前文件对象上调用renderFolder。

if (objects[i].isDirectory() == false) {
        indent(sb, level, isLast, hierarchyTree).append(objects[i].getName()).append("\n");
        hierarchyTree.remove(hierarchyTree.size() - 1);
        continue;
}

在我的测试文件夹中对其进行测试导致以下结果:

└──62476474
    ├──target
    │   ├──generated-sources
    │   │   └──annotations
    │   ├──classes
    │   │   ├──Main.class
    │   └──maven-status
    │       └──maven-compiler-plugin
    │           └──compile
    │               └──default-compile
    │                   └──inputFiles.lst
    │                   └──createdFiles.lst
    └──pom.xml
    └──src
        ├──test
        │   └──java
        └──main
            └──java
                └──Main.java

这是我的文件夹结构的准确表示。

这应该可以解决您的问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章