java获取文件夹容量大小,带进度条

package test;

import java.io.File;

/**
 * <p>描述: 文件维护工具类
 * 主要包括:获取文件夹容量大小
 * </p>
 * <p>创建时间: 2021/2/5 </p>
 *
 * @author hzz
 * @version 1.0
 */
public class FileDelUtil {

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        File file = new File("D:\\output\\idea\\elasticsearch");
        System.out.println("所在盘空间总大小:" + sizeFormat(file.getTotalSpace()));   //所在盘空间总大小
        System.out.println("所在盘空间可用大小:" + sizeFormat(file.getUsableSpace()));  //所在盘空间总可用大小
        System.out.println("所在盘空间总未占用大小:" + sizeFormat(file.getFreeSpace()));    //所在盘空间总未占用大小
        System.out.println("文件夹所用大小:" + sizeFormat(fileTotalSize(file)));
        System.out.println("总耗时:" + timeFormat(System.currentTimeMillis() - start));
    }

    public static String timeFormat(long input){
        return input / (3600 * 1000) +"小时" + ((input / 1000) % 3600) / 60 + "分钟" + (input / 1000) % 60 + "秒";
    }

    public static String sizeFormat(long input){
        if (input < pow1024(1)){
            return input + "B";
        }else if (input >= pow1024(1) && input < pow1024(2)){
            return input / pow1024(1)  + "KB";
        }else if (input >= pow1024(2) && input < pow1024(3)){
            return input / pow1024(2)  + "MB";
        }else if (input >= pow1024(3) && input < pow1024(4)){
            return input / pow1024(3)  + "GB";
        }else if (input >= pow1024(4) && input < pow1024(5)){
            return input / pow1024(4)  + "TB";
        }else {
            return input + "B";
        }
    }

    static long total = 0;
    static long cur = 0;

    public static long fileTotalSize(File file){
        long result = 0;
        if (file != null){
            total ++;
            cur ++;
            if (file.isDirectory()){
                File[] files = file.listFiles();
                total += files.length;
                assert files != null;
                for (File temp: files){
                    cur ++;
                    result += fileTotalSize(temp);
                    printSchedule(cur, total);
                }
            }else {
                return file.length();
            }
        }
        return result;
    }

    /**
     * 百分比整型表示, 0-100
     * @param cur
     * @param total
     * @return
     */
    public static int intPercent(long cur, long total){
        double result = (double) cur / (double) total * 100;
        return new Double(Math.floor(result)).intValue();
    }

    public static long pow1024(int num){
        long result = 1;
        for (int i = 0; i < num; i ++){
            result = result << 10;
        }
        return result;
    }

    /**
     * 进度条总长度
     */
    private static int TOTAL_LENGTH = 30;
    public static void printSchedule(long cur, long total){
        int percentInt = intPercent(cur, total);
        for (int i = 0; i < TOTAL_LENGTH + 30; i++) {
            System.out.print("\b");
        }
        //░▒
        int now = TOTAL_LENGTH * percentInt / 100;
        for (int i = 0; i < now; i++) {
            System.out.print("▒");
        }
        for (int i = 0; i < TOTAL_LENGTH - now; i++) {
            System.out.print(" ");
        }
        System.out.print("  " + percentInt + "%");
        System.out.print("  " + cur + "/" + total);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值