【问题标题】:convert byte size into human readable format in Java?在Java中将字节大小转换为人类可读的格式?
【发布时间】:2020-04-21 04:34:55
【问题描述】:

我正在尝试创建一个静态方法String formatSize(long sizeInBytes) 此方法需要为提供的文件大小(以字节为单位)返回最合适的表示 转换为至少有 2 位小数(字节除外)的人类可读格式。

这是我的代码

public class HexEditor {

    public static void main(String[] args) {
        System.out.println(formatSize(2147483647));
        System.out.println(formatSize(123));
        System.out.println(formatSize(83647));
        System.out.println(formatSize(9585631));
        System.out.println(formatSize(188900977659375L));
    }
    public static String floatForm (double d){
       return new DecimalFormat("#.##").format(d);
    }

    public static String formatSize(long size) {


        double B = 1 * 8;
        double kibit = 1024;
        double KiB = B * kibit;
        double MiB = KiB * kibit;
        double GiB = MiB * kibit;
        double TiB = GiB * kibit;
        double Pib = TiB * kibit;

        if (size < kibit) {
            return size + " byte";

        } else if (size < KiB) {
            double result = size / kibit;
            return floatForm (result) + " Kibit";

        } else if (size < MiB) {
            double result = size / KiB;
            return floatForm (result) + " KiB";

        } else if (size < GiB) {
            double result = size / MiB;
            return floatForm (result) + " MiB";

        } else if (size < TiB) {
            double result = size / GiB;
            return floatForm (result) + " GiB";

        } else if (size < Pib) {
            double result = size / TiB;
            return floatForm (result) + " TiB";
        }

        return "";
    }

}

这些是我的输入和期望输出

输入输出

2147483647          2.00 GiB
123                 123 bytes
83647               81.69 KiB
9585631             9.14 MiB
188900977659375     171.80 TiB

但是当我的代码运行时,它会给出不同的输出

    256 MiB
    123 byte
    10.21 KiB
    1.14 MiB
    21.48 TiB

我错了吗?什么的

【问题讨论】:

  • 其实你做错了。最大的字节输入应该在 if 语句中,第二大的应该在 else if 语句中,依此类推。希望对您有所帮助。

标签: java formatting number-formatting


【解决方案1】:

您正在按位除,但您的输入已经以字节为单位,而不是位。因此,除了

试一试以下代码(添加打印语句只是为了检查除数):

        double KiB = Math.pow(2, 10);
        double MiB = Math.pow(2, 20);
        double GiB = Math.pow(2, 30);
        double TiB = Math.pow(2, 40);
        double Pib = Math.pow(2, 50);

        NumberFormat df = DecimalFormat.getInstance();
        System.out.println("KiB: " + df.format(KiB));
        System.out.println("MiB: " + df.format(MiB));
        System.out.println("GiB: " + df.format(GiB));
        System.out.println("TiB: " + df.format(TiB));
        System.out.println("Pib: " + df.format(Pib));

        if (size < KiB) {
            return size + " byte";

        } else if (size < MiB) {
            double result = size / KiB;
            return floatForm(result) + " KiB";

        /* remaining code is identical to yours */

【讨论】:

  • 顺便说一句,我认为这在这里不适用,但当然有可用的库可以为您完成这项工作。 Apache Commons FileUtils 例如:FileUtils.byteCountToDisplaySize(size)
  • 谢谢,朋友,其实我的 KiB 计算是错误的
  • NP 很高兴为您提供帮助。是的,这一切都很好,你只是不需要B 作为一个因素......
猜你喜欢
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 2012-05-12
相关资源
最近更新 更多