如何在java中将字节大小转换为人类可读的格式?

如何在java中将字节大小转换为人类可读的格式?

如何在Java中将字节大小转换为人类可读的格式?像1024应该变成“1 Kb”而1024 * 1024应该变成“1 Mb”。

我有点厌倦为每个项目编写这个实用工具方法。Apache Commons中是否有任何静态方法?


临摹微笑
浏览 872回答 3
3回答

慕田峪9158850

这是我的去处(没有循环并处理SI单位和二进制单位):public&nbsp;static&nbsp;String&nbsp;humanReadableByteCount(long&nbsp;bytes,&nbsp;boolean&nbsp;si)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;unit&nbsp;=&nbsp;si&nbsp;?&nbsp;1000&nbsp;:&nbsp;1024; &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(bytes&nbsp;<&nbsp;unit)&nbsp;return&nbsp;bytes&nbsp;+&nbsp;"&nbsp;B"; &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;exp&nbsp;=&nbsp;(int)&nbsp;(Math.log(bytes)&nbsp;/&nbsp;Math.log(unit)); &nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;pre&nbsp;=&nbsp;(si&nbsp;?&nbsp;"kMGTPE"&nbsp;:&nbsp;"KMGTPE").charAt(exp-1)&nbsp;+&nbsp;(si&nbsp;?&nbsp;""&nbsp;:&nbsp;"i"); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;String.format("%.1f&nbsp;%sB",&nbsp;bytes&nbsp;/&nbsp;Math.pow(unit,&nbsp;exp),&nbsp;pre);}示例输出:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SI&nbsp; &nbsp; &nbsp;BINARY&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0:&nbsp; &nbsp; &nbsp; &nbsp; 0 B&nbsp; &nbsp; &nbsp; &nbsp; 0 B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 27:&nbsp; &nbsp; &nbsp; &nbsp;27 B&nbsp; &nbsp; &nbsp; &nbsp;27 B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;999:&nbsp; &nbsp; &nbsp; 999 B&nbsp; &nbsp; &nbsp; 999 B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1000:&nbsp; &nbsp; &nbsp;1.0 kB&nbsp; &nbsp; &nbsp;1000 B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1023:&nbsp; &nbsp; &nbsp;1.0 kB&nbsp; &nbsp; &nbsp;1023 B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1024:&nbsp; &nbsp; &nbsp;1.0 kB&nbsp; &nbsp; 1.0 KiB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1728:&nbsp; &nbsp; &nbsp;1.7 kB&nbsp; &nbsp; 1.7 KiB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 110592:&nbsp; &nbsp;110.6 kB&nbsp; 108.0 KiB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;7077888:&nbsp; &nbsp; &nbsp;7.1 MB&nbsp; &nbsp; 6.8 MiB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;452984832:&nbsp; &nbsp;453.0 MB&nbsp; 432.0 MiB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;28991029248:&nbsp; &nbsp; 29.0 GB&nbsp; &nbsp;27.0 GiB&nbsp; &nbsp; &nbsp; &nbsp;1855425871872:&nbsp; &nbsp; &nbsp;1.9 TB&nbsp; &nbsp; 1.7 TiB&nbsp;9223372036854775807:&nbsp; &nbsp; &nbsp;9.2 EB&nbsp; &nbsp; 8.0 EiB&nbsp; &nbsp;(Long.MAX_VALUE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SI&nbsp; &nbsp; &nbsp;BINARY&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0:&nbsp; &nbsp; &nbsp; &nbsp; 0 B&nbsp; &nbsp; &nbsp; &nbsp; 0 B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 27:&nbsp; &nbsp; &nbsp; &nbsp;27 B&nbsp; &nbsp; &nbsp; &nbsp;27 B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;999:&nbsp; &nbsp; &nbsp; 999 B&nbsp; &nbsp; &nbsp; 999 B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1000:&nbsp; &nbsp; &nbsp;1.0 kB&nbsp; &nbsp; &nbsp;1000 B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1023:&nbsp; &nbsp; &nbsp;1.0 kB&nbsp; &nbsp; &nbsp;1023 B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1024:&nbsp; &nbsp; &nbsp;1.0 kB&nbsp; &nbsp; 1.0 KiB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1728:&nbsp; &nbsp; &nbsp;1.7 kB&nbsp; &nbsp; 1.7 KiB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 110592:&nbsp; &nbsp;110.6 kB&nbsp; 108.0 KiB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;7077888:&nbsp; &nbsp; &nbsp;7.1 MB&nbsp; &nbsp; 6.8 MiB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;452984832:&nbsp; &nbsp;453.0 MB&nbsp; 432.0 MiB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;28991029248:&nbsp; &nbsp; 29.0 GB&nbsp; &nbsp;27.0 GiB&nbsp; &nbsp; &nbsp; &nbsp;1855425871872:&nbsp; &nbsp; &nbsp;1.9 TB&nbsp; &nbsp; 1.7 TiB&nbsp;9223372036854775807:&nbsp; &nbsp; &nbsp;9.2 EB&nbsp; &nbsp; 8.0 EiB&nbsp; &nbsp;(Long.MAX_VALUE)相关文章:Java:将字节大小格式化为人类可读格式

白猪掌柜的

使用Android内置类对于Android,有一个类Formatter。就像代码一样,你就完成了。android.text.format.Formatter.formatShortFileSize(activityContext,&nbsp;bytes);它是formatFileSize(),但尝试生成更短的数字(显示更少的小数)。android.text.format.Formatter.formatFileSize(activityContext,&nbsp;bytes);格式化内容大小为字节,千字节,兆字节等形式。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
Android