如何在Java中将字节大小转换为人类可读的格式?
像1024应该变成“ 1 Kb”,而1024 * 1024应该变成“ 1 Mb”。
我有点讨厌为每个项目编写此实用程序方法。在Apache Commons中有一个静态方法吗?
有趣的事实:此处发布的原始代码段是有史以来复制最多的Java代码段,并且存在缺陷。它是固定的,但变得混乱。
本文全文:有史以来复制最多的Stack Overflow片段存在缺陷!
来源:将字节大小格式化为人类可读格式 编程指南
SI(1 k = 1,000) public static String humanReadableByteCountSI(long bytes) { if (-1000 < bytes && bytes < 1000) { return bytes + " B"; } CharacterIterator ci = new StringCharacterIterator("kMGTPE"); while (bytes <= -999_950 || bytes >= 999_950) { bytes /= 1000; ci.next(); } return String.format("%.1f %cB", bytes / 1000.0, ci.current()); }
二进制(1 K = 1,024)
public static String humanReadableByteCountBin(long bytes) { long absB = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes); if (absB < 1024) { return bytes + " B"; } long value = absB; CharacterIterator ci = new StringCharacterIterator("KMGTPE"); for (int i = 40; i >= 0 && absB > 0xfffccccccccccccL >> i; i -= 10) { value >>= 10; ci.next(); } value *= Long.signum(bytes); return String.format("%.1f %ciB", value / 1024.0, ci.current()); }
输出示例:
SI BINARY 0: 0 B 0 B 27: 27 B 27 B 999: 999 B 999 B 1000: 1.0 kB 1000 B 1023: 1.0 kB 1023 B 1024: 1.0 kB 1.0 KiB 1728: 1.7 kB 1.7 KiB 110592: 110.6 kB 108.0 KiB 7077888: 7.1 MB 6.8 MiB 452984832: 453.0 MB 432.0 MiB 28991029248: 29.0 GB 27.0 GiB 1855425871872: 1.9 TB 1.7 TiB 9223372036854775807: 9.2 EB 8.0 EiB (Long.MAX_VALUE)