当前位置: 代码网 > it编程>编程语言>Java > Java文件大小转换的两种方式小结

Java文件大小转换的两种方式小结

2024年07月22日 Java 我要评论
在程序开发的过程中,文件的大小在视图呈现和数据库存储的过程不一致怎么转换呢文件大小的单位,在计算机中,文件大小通常使用字节(byte)作为基本单位进行表示。字节是计算机存储最小的单位,每个字节表示8个

在程序开发的过程中,文件的大小在视图呈现和数据库存储的过程不一致怎么转换呢

文件大小的单位,在计算机中,文件大小通常使用字节(byte)作为基本单位进行表示。字节是计算机存储最小的单位,每个字节表示8个二进制位(bit)。除了字节,还有一些常用的文件大小单位,如下所示:

千字节(kb):1 kb = 1024 字节
兆字节(mb):1 mb = 1024 kb
吉字节(gb):1 gb = 1024 mb
太字节(tb):1 tb = 1024 gb

方式一

使用第三方依赖库 apache commons io 提供的方法

添加以下依赖

<dependency>
    <groupid>org.apache.commons</groupid>
    <artifactid>commons-io</artifactid>
    <version>1.3.2</version>
</dependency>

代码示例

import org.apache.commons.io.fileutils;
public class testfilesize {

    public static void main(string[] args) {
        long filesize = 33931l;
        string filesizestr = fileutils.bytecounttodisplaysize(filesize);
        system.out.println("文件大小:" + filesizestr);
    }
}

方式二

自己添加代码写,以下几种转换方式的不同写法,基本都一样,可根据自己需求删减

import java.text.decimalformat;

import org.apache.commons.io.fileutils;

public class testfilesize {

    public static void main(string[] args) {
        long bytes = 33931l;
        string filesizestr = fileutils.bytecounttodisplaysize(bytes);
        system.out.println("文件大小:" + filesizestr);

        // 自己定义方法实现
        string filesizestr1 = formatfilesize1(bytes);
        system.out.println("文件转换字符转大小方式一:" + filesizestr1);
        string filesizestr2 = formatfilesize2(bytes);
        system.out.println("文件转换字符转大小方式二:" + filesizestr2);
        string filesizestr3 = formatfilesize3(bytes);
        system.out.println("文件转换字符转大小方式三:" + filesizestr3);
        // 转换为字节
        long filebytes1 = convertsizetolong1(filesizestr2);
        system.out.println("字节数为:" + filebytes1);
        long filebytes2 = convertsizetolong2(filesizestr2);
        system.out.println("字节数为:" + filebytes2);
    }


    /**
     * 将字节数转换为其他单位的文件大小
     *
     * @param bytes 字节数
     * @return 转换后的文件大小
     */
    public static string formatfilesize1(long bytes) {
        if (bytes < 1024) {
            return bytes + " b";
        } else if (bytes < 1024 * 1024) {
            return math.round(bytes / 1024.0) + " kb";
        } else if (bytes < 1024 * 1024 * 1024) {
            return math.round(bytes / (1024.0 * 1024.0)) + " mb";
        } else {
            return math.round(bytes / (1024.0 * 1024.0 * 1024.0)) + " gb";
        }
    }

    /**
     * 将字节数转换为其他单位的文件大小
     *
     * @param bytes 字节数
     * @return 转换后的文件大小
     */
    private static string formatfilesize2(long bytes) {
        decimalformat df = new decimalformat("#.00");
        string filesizestring = "";
        string wrongsize = "0 b";
        if (bytes == 0) {
            return wrongsize;
        }
        if (bytes < 1024) {
            filesizestring = df.format((double) bytes) + " b";
        } else if (bytes < 1048576) {
            filesizestring = df.format((double) bytes / 1024) + " kb";
        } else if (bytes < 1073741824) {
            filesizestring = df.format((double) bytes / 1048576) + " mb";
        } else {
            filesizestring = df.format((double) bytes / 1073741824) + " gb";
        }
        return filesizestring;
    }

    /**
     * 将字节数转换为其他单位的文件大小
     *
     * @param bytes 字节数
     * @return 转换后的文件大小
     */
    public static string formatfilesize3(long bytes) {
        string[] units = {"bytes", "kb", "mb", "gb", "tb"};
        int unitindex = 0;
        while (bytes > 1024 && unitindex < units.length - 1) {
            bytes /= 1024;
            unitindex++;
        }
        return bytes + " " + units[unitindex];
    }

    /**
     * 将其他单位的文件大小转换为字节数
     *
     * @param size 文件大小(带单位)
     * @return 转换后的字节数
     */
    public static long convertsizetolong1(string size) {
        long factor = 1;
        string unit = size.substring(size.length() - 2).trim().touppercase();
        // 截取到小数点最后一位
        long value = long.parselong(size.substring(0, size.lastindexof(".")).trim());
        if (unit.equals("kb")) {
            factor = 1024;
        } else if (unit.equals("mb")) {
            factor = 1024 * 1024;
        } else if (unit.equals("gb")) {
            factor = 1024 * 1024 * 1024;
        }

        return value * factor;
    }

    /**
     * 将其他单位的文件大小转换为字节数
     *
     * @param size 文件大小(带单位)
     * @return 转换后的字节数
     */
    public static long convertsizetolong2(string size) {
        long factor = 1;
        size = size.trim().touppercase();
        string unit = size.replaceall("[^a-za-z]+", "");
        double value = double.parsedouble(size.replaceall("[^0-9.]+", "").replace(unit, ""));
        switch (unit) {
            case "kb":
                factor = 1024;
                break;
            case "mb":
                factor = 1024 * 1024;
                break;
            case "gb":
                factor = 1024 * 1024 * 1024;
                break;
        }

        return (long) value * factor;
    }
}

到此这篇关于java文件大小转换的两种方式小结的文章就介绍到这了,更多相关java文件大小转换内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com