Java将文件字节大小转换为KB,MB,GB

查找自stackoverflow



public static String readableFileSize(long size) {

    if(size <= 0) return "0";

    final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };

    int digitGroups = (int) (Math.log10(size)/Math.log10(1024));

    return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];

}

https://stackoverflow.com/questions/3263892/format-file-size-as-mb-gb-etc

你可能感兴趣的:(Java将文件字节大小转换为KB,MB,GB)