Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将字节大小转换为人类可读的格式?_Java_Formatting_Number Formatting - Fatal编程技术网

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

在Java中将字节大小转换为人类可读的格式?,java,formatting,number-formatting,Java,Formatting,Number Formatting,我正在尝试创建一个静态方法String formatSize(long sizeInBytes) 此方法需要返回所提供文件大小的最合适表示形式(以字节为单位) 至少有2位小数的人类可读格式(字节除外) 这是我的密码 public class HexEditor { public static void main(String[] args) { System.out.println(formatSize(2147483647)); System.out.

我正在尝试创建一个静态方法
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 "";
    }

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

    256 MiB
    123 byte
    10.21 KiB
    1.14 MiB
    21.48 TiB

我错了吗?或者你正在除以位,但是你的输入已经是字节了,不是位了。因此,除了 对以下代码进行一次旋转(仅为检查除数而添加的打印语句):

double KiB=Math.pow(2,10);
双MiB=数学功率(2,20);
双GiB=数学功率(2,30);
双TiB=数学功率(2,40);
双Pib=数学功率(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语句中,第二大字节输入应该在else if语句中,依此类推。希望能有所帮助。顺便说一句,我假设这在这里不适用,但当然有可用的库可以帮您完成这项工作。Apache Commons
FileUtils
例如:
FileUtils.byteCountToDisplaySize(size)
谢谢,朋友,实际上我的KiB计算是错误的,我很高兴能帮上忙。是的,一切都很好,你只是不需要
B
作为一个因素。。。
    256 MiB
    123 byte
    10.21 KiB
    1.14 MiB
    21.48 TiB