Java实现Tree命令

package util.file;

import java.io.File;

/**
 * 搜索文件.
 *
 * @author koveer
 * @since 1.0  - 2023/4/9 18:53
 */
public class SearchFile {

    public static void main(String[] args) {
        String directoryPath = "D:\\CS\\Markdown笔记"; // 指定目录路径
        File rootDir = new File(directoryPath);
        printTree(rootDir,1);
    }


    /**
     * 返回文件大小的字符串形式,level指定文件大小格式 0:Byte, 1:KB, 2:MB, 3:GB.
     *
     * @param file  文件路径
     * @param level 文件大小格式
     * @return java.lang.String string
     * @author koveer  -2023/4/11 21:54
     * @since 1.0
     */
    public static String FileLevelSizeString(File file, int level) {
        String[] levelString = new String[]{"Byte", "KB", "MB", "GB"};
        return "(%d %s)".formatted(FileLevelSize(file, level), levelString[level]);
    }

    /**
     * 格式化输出文件或文件夹大小,level指定文件大小格式 0:Byte, 1:KB, 2:MB, 3:GB.
     *
     * @param file  文件路径
     * @param level 文件大小格式
     * @return long
     * @author koveer
     * -2023/4/9 20:33
     * @since 1.0
     */
    public static long FileLevelSize(File file, int level) {
        switch (level) {
            case 0 -> {
                return FileSize(file);
            }
            case 1 -> {
//                return FileSize(file) / 1024;
                return FileSize(file) >> 10;
            }
            case 2 -> {
//                return FileSize(file) / 1048576;
                return FileSize(file) >> 20;
            }
            case 3 -> {
//                return FileSize(file) / 1073741824;
                return FileSize(file) >> 30;
            }
            default -> throw new IllegalStateException("Unexpected value: " + level);
        }

    }

    /**
     * 递归获取文件或文件夹大小.
     *
     * @param file 文件路径
     * @return long
     * @author koveer
     * -2023/4/9 20:32
     * @since 1.0
     */
    public static long FileSize(File file) {
        if (file.isFile()) {
            return file.length();
        } else {
            long size = 0;
            File[] fileList = file.listFiles();
            if (fileList != null)
                for (File f : fileList) {
                    if (f.isFile()) {
                        size += f.length();
                    } else {
                        size += FileSize(f);
                    }
                }
            return size;
        }
    }

    /**
     * 以树状图形式打印文件夹下文件及大小,默认MB.
     *
     * @param file 文件路径
     * @author koveer
     * -2023/4/9 20:40
     * @since 1.0
     */
    public static void printTree(File file) {
        System.out.println(file);
        printTree(file, "", 0, 2);
    }

    /**
     * 以树状图形式打印文件夹下文件及大小.
     *
     * @param file  文件路径
     * @param level 大小格式
     * @author koveer
     * -2023/4/9 20:40
     * @since 1.0
     */
    public static void printTree(File file, int level) {
        System.out.println(file);
        printTree(file, "", 0, level);
    }

    /**
     * 以树状图形式打印文件夹下文件及大小,默认MB.
     *
     * @param file   文件路径
     * @param prefix 文件前缀
     * @param deep   文件深度
     * @author koveer
     * -2023/4/9 20:39
     * @since 1.0
     */
    private static void printTree(File file, String prefix, int deep, int level) {
        if (!file.isDirectory()) {
            System.out.println("无效的文件路径.");
        } else {
            // 生成目录下的子文件和子目录列表
            File[] fileList = file.listFiles();
            String thisPrefix = "";
            String nextPrefix = "";
            for (int i = 0; i < fileList.length; i++) {
                if (deep >= 0) {
                    // 如果不是最后一个元素
                    if ((i + 1 < fileList.length)) {
                        // 不是最后一个文件/目录都打印这个符号
                        thisPrefix = prefix + "├─";
                        // 下一个打印这符号表示展开目录
                        nextPrefix = prefix + "│ ";
                    } else {
                        // 最后一个子文件/子目录项
                        thisPrefix = prefix + "└─";
                        //
                        nextPrefix = prefix + "  ";
                    }
                }
                // 如果是文件,则输出文件名和文件大小
                if (fileList[i].isFile()) {
                    System.out.printf("%s%s    %s%n", thisPrefix, fileList[i].getName(), FileLevelSizeString(file, level));
                }
                // 如果是目录,则输出目录名
                if (fileList[i].isDirectory()) {
                    System.out.printf("%s\u001B[36m%s    %s\u001B[0m%n", thisPrefix, fileList[i].getName(), FileLevelSizeString(file, level));
                    printTree(fileList[i], nextPrefix, deep + 1, level); // 递归输出目录下的文件和目录
                }
            }
        }
    }
}

具体效果:
以树状图的形式展现文件夹下所有文件及其大小,文件夹会标记为蓝色,实测效率不如windows自带的tree命令,但可拓展性比较高。

在控制台显示颜色的效果在WINDOWS10及以下的CMD窗口无法实现,请使用更高级的控制台,如terminal或者IDEA等。

请使用JDK12及以上版本。每个方法的具体功能已经详细注释。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值