打印一个目录下的所有文件

那年仲夏 提交于 2019-12-28 00:40:23

打印一个目录下的所有文件

例如我有这么一个文件

package package1222;

import java.io.File;
public class Main {
    private static void listDirectory(File root){
        File[] files = root.listFiles();
        if (files == null || files.length == 0){
            return;
        }
        for (File file : files){
            System.out.println(file.getAbsolutePath());
            if (file.isDirectory()){
                //递归
                listDirectory(file);
            }
        }
    }
    public static void main(String[] args) {
        File root = new File("D:\\IO学习");
        File[] files = root.listFiles();
        listDirectory(root);
    }
}

D:\IO学习\hello1.txt
D:\IO学习\hello2.java
D:\IO学习\hello3.c
D:\IO学习\我是一个子文件夹
D:\IO学习\我是一个子文件夹\我在子文件.java


进化版

package package1222;

import java.io.File;

public class Main {
    private static void listDirectory(File root,int level){
        File[] files = root.listFiles();
        if (files == null || files.length == 0){
            return;
        }
        for (File file : files){
            for (int i = 0; i < level*2; i++) {
                System.out.print("  ");
            }
            System.out.print(file.getName());
            if (file.isDirectory()){
                System.out.println(File.separator);//表示斜杠
            }else {
                System.out.println(" " + file.length());
            }
            if (file.isDirectory()){
                //递归
                listDirectory(file,level + 1);
            }
        }
    }
    public static void main(String[] args) {
        File root = new File("D:\\IO学习");
        File[] files = root.listFiles();
        System.out.println(root.getAbsolutePath());
        listDirectory(root,1);
    }
}
D:\IO学习
    hello1.txt 22
    hello2.java 21
    hello3.c 0
    我是一个子文件夹\
        我在子文件.java 11
        我还是一个文件夹\
            hello4.sql 0
    我是子文件夹2\
    我是子文件夹3\

以上是深度优先遍历(类似 二叉树的前序,中序,后序)
下面写一种广度遍历(用队列来描述)


class ListDirecory{
    private static class Node{
        File file;
        int level;

        public Node(File file, int level) {
            this.file = file;
            this.level = level;
        }
    }
    public static void main(String[] args) {
        File root = new File("D:\\IO学习");
        Queue<Node> queue = new LinkedList<>();//只有LinkedList可以当队列使用
        queue.offer(new Node(root,0));
        while (!queue.isEmpty()){
            Node node = queue.poll();
            File front = node.file;
            for (int i = 0; i < node.level; i++) {
                System.out.print("  ");
            }
            System.out.println(front.getName());
            if (!front.isDirectory()){
                continue;
            }
            //是孩子
            File[] files = front.listFiles();
            if (files == null || files.length == 0){
                continue;
            }


            for (File file : files){
                queue.offer(new Node(file,node.level + 1));
            }
        }
    }
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!