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

本文介绍了如何打印一个目录及其子目录下的所有文件,包括深度优先遍历和广度优先遍历的方法。例如,展示了如何处理D:IO学习目录中的hello1.txt、hello2.java、hello3.c以及子文件夹中的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

例如我有这么一个文件

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));
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值