Java基于byte length参数生成易于理解的文件大小

commons-io工具类FileUtils中有一个方法,FileUtils.byteCountToDisplaySize => 以可读的方式,返回文件的大小EB, PB, TB, GB, MB, KB or bytes,但是该方法返回的是整数,如果满足需求可直接用,本人觉得保留两位小数在实际应用中更合理一些,因此在系统中新增FileUtils类,重写该方法。

package cn.common.util;

import java.math.BigDecimal;
import java.math.BigInteger;

public class FileUtils {

    public static final long ONE_KB = 1024;

    public static final BigInteger ONE_KB_BI = BigInteger.valueOf(ONE_KB);

    public static final BigInteger ONE_MB_BI = ONE_KB_BI.multiply(ONE_KB_BI);

    public static final BigInteger ONE_GB_BI = ONE_KB_BI.multiply(ONE_MB_BI);

    public static final BigInteger ONE_TB_BI = ONE_KB_BI.multiply(ONE_GB_BI);

    public static final BigInteger ONE_PB_BI = ONE_KB_BI.multiply(ONE_TB_BI);

    public static final BigInteger ONE_EB_BI = ONE_KB_BI.multiply(ONE_PB_BI);

    public static String byteCountToDisplaySize(final long longSize) {
        BigInteger size = BigInteger.valueOf(longSize);
        String displaySize;

        if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0) {
            displaySize = divide(size, ONE_EB_BI) + " EB";
        } else if (size.divide(ONE_PB_BI).compareTo(BigInteger.ZERO) > 0) {
            displaySize = divide(size, ONE_PB_BI) + " PB";
        } else if (size.divide(ONE_TB_BI).compareTo(BigInteger.ZERO) > 0) {
            displaySize = divide(size, ONE_TB_BI) + " TB";
        } else if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) {
            displaySize = divide(size, ONE_GB_BI) + " GB";
        } else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) {
            displaySize = divide(size, ONE_MB_BI) + " MB";
        } else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) {
            displaySize = divide(size, ONE_KB_BI) + " KB";
        } else {
            displaySize = size + " bytes";
        }
        return displaySize;
    }

    public static double divide(BigInteger size, BigInteger one_bi) {
        BigDecimal decimalSize = BigDecimal.valueOf(size.doubleValue())
                .divide(BigDecimal.valueOf(one_bi.doubleValue()), 2, BigDecimal.ROUND_HALF_UP);
        return decimalSize.doubleValue();
    }
}

因代码较长显示不全问题,可以直接点击右上角复制代码。

你可能感兴趣的:(Java)