格式化文件大小为MB,GB等

Sea*_*oyd 120 java string format numbers

我需要使用合理的单位将文件大小显示为String.

例如

1L ==> "1 B";
1024L ==> "1 KB";
2537253L ==> "2.3 MB"
Run Code Online (Sandbox Code Playgroud)

等等

我找到了之前的答案,我觉得并不令人满意

我提出了自己的解决方案,它有类似的缺点:

private static final long K = 1024;
private static final long M = K * K;
private static final long G = M * K;
private static final long T = G * K;

public static String convertToStringRepresentation(final long value){
    final long[] dividers = new long[] { T, G, M, K, 1 };
    final String[] units = new String[] { "TB", "GB", "MB", "KB", "B" };
    if(value < 1)
        throw new IllegalArgumentException("Invalid file size: " + value);
    String result = null;
    for(int i = 0; i < dividers.length; i++){
        final long divider = dividers[i];
        if(value >= divider){
            result = format(value, divider, units[i]);
            break;
        }
    }
    return result;
}

private static String format(final long value,
    final long divider,
    final String unit){
    final double result =
        divider > 1 ? (double) value / (double) divider : (double) value;
    return String.format("%.1f %s", Double.valueOf(result), unit);
}
Run Code Online (Sandbox Code Playgroud)

主要问题是我对Decimalformat和/或String.format的了解有限.我想将1024L,1025L等映射到1 KB而不是1.0 KB.

那么,有两种可能性:

  1. 我更喜欢公共图书馆中的一个开箱即用的解决方案,如apache commons或google guava.
  2. 如果没有,有人可以告诉我如何摆脱'.0'部分(不使用字符串替换和正则表达式,我可以自己做)

Mr *_* Ed 364

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];
}
Run Code Online (Sandbox Code Playgroud)

这将达到1000 TB ....而且程序很短!

  • 符合国际标准:final String [] units = new String [] {"B","KB","MB","GB","TB","PB","EB"};/*用于1000*/final String [] units = new String [] {"Bi","KiB","MiB","GiB","TiB","PiB","EiB"};/*与1024*一起使用/参见:http://physics.nist.gov/cuu/Units/binary.html (26认同)
  • final String [] units = new String [] {"B","KB","MB","GB","TB","EB"}; //现在它可以达到Long.MAX_VALUE! (4认同)
  • 同意,应该是正确的答案.查看[here](http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java/3758880#3758880)以获得更好的输出. (3认同)
  • 和反向功能?... (3认同)
  • 一个小评论:"kilo"用小写的"k"表示而不是大写的"K". (2认同)
  • 给 Android 开发人员的一个提示:要在 Android 上以一种很好的可读方式显示文件大小,您可以使用 `Formatter.formatShortFileSize(context,file.length())` (2认同)

Rob*_*ins 8

你可能会有更多的运气java.text.DecimalFormat.这个代码可能应该这样做(虽然它只是翼了...)

new DecimalFormat("#,##0.#").format(value) + " " + unit


小智 5

这让我很惊讶,但基于循环的算法要快 10% 左右。

public static String toNumInUnits(long bytes) {
    int u = 0;
    for ( ; bytes > 1024*1024; bytes >>= 10) {
        u++;
    }
    if (bytes > 1024)
        u++;
    return String.format("%.1f %cB", bytes/1024f, " kMGTPE".charAt(u));
}
Run Code Online (Sandbox Code Playgroud)

  • 一切都是基于循环的底部。 (4认同)
  • **警告**:这对于字节 &lt;= 1024 不起作用(结果始终为 0.0 B)。您需要添加 `if (bytes &lt;= 1024) { return String.format("%.1f B", (float) bytes); }` 在方法的开头。 (2认同)