Java使用hutool實(shí)現(xiàn)文件大小的友好輸出
文檔
基本使用
依賴(lài)
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.22</version>
</dependency>
示例
package com.example.demo;
import org.junit.Test;
import cn.hutool.core.io.unit.DataSizeUtil;
public class DataSizeTests {
@Test
public void testDataSize() {
long b = 1L;
long kb = 1024L + 512L;
long mb = 1024L * 1024L;
long gb = 1024L * 1024L * 1024L;
long tb = 1024L * 1024L * 1024L * 1024L;
System.out.println(DataSizeUtil.format(b)); // 1 B
System.out.println(DataSizeUtil.format(kb)); // 1.5 KB
System.out.println(DataSizeUtil.format(mb)); // 1 MB
System.out.println(DataSizeUtil.format(gb)); // 1 GB
System.out.println(DataSizeUtil.format(tb)); // 1 TB
}
}
代碼實(shí)現(xiàn)
看下他的實(shí)現(xiàn)方式
package cn.hutool.core.io.unit;
import java.text.DecimalFormat;
/**
* 數(shù)據(jù)大小工具類(lèi)
*
* @author looly
* @since 5.3.10
*/
public class DataSizeUtil {
/**
* 解析數(shù)據(jù)大小字符串,轉(zhuǎn)換為bytes大小
*
* @param text 數(shù)據(jù)大小字符串,類(lèi)似于:12KB, 5MB等
* @return bytes大小
*/
public static long parse(String text) {
return DataSize.parse(text).toBytes();
}
/**
* 可讀的文件大小<br>
* 參考 http://stackoverflow.com/questions/3263892/format-file-size-as-mb-gb-etc
*
* @param size Long類(lèi)型大小
* @return 大小
*/
public static String format(long size) {
if (size <= 0) {
return "0";
}
int digitGroups = Math.min(DataUnit.UNIT_NAMES.length-1, (int) (Math.log10(size) / Math.log10(1024)));
return new DecimalFormat("#,##0.##")
.format(size / Math.pow(1024, digitGroups)) + " " + DataUnit.UNIT_NAMES[digitGroups];
}
}可以看到format方法,取了1204為底的對(duì)數(shù),代碼很簡(jiǎn)潔
自定義實(shí)現(xiàn)代碼
package com.example.demo;
import java.text.DecimalFormat;
public class DataSizeUtil {
// 單位大小
public static final int UNIT_SIZE = 1024;
// 顯示單位
public static final String[] UNIT_NAMES = new String[]{
"B", "KB", "MB", "GB", "TB", "PB", "EB"
};
/**
* 可讀的文件大小
*
* @param size long
* @return
*/
public static String format(long size) {
if (size <= 0) {
return "0";
}
int digitGroups = Math.min(UNIT_NAMES.length - 1, (int) (Math.log10(size) / Math.log10(UNIT_SIZE)));
String value = new DecimalFormat("#.#").format(size / Math.pow(UNIT_SIZE, digitGroups));
return String.format("%s %s", value, UNIT_NAMES[digitGroups]);
}
}
補(bǔ)充知識(shí)
換底公式
loga?b=logc?b÷logc?a
DecimalFormat
DecimalFormat 用于數(shù)字格式化
package com.example.demo;
import org.junit.Test;
import java.text.DecimalFormat;
public class DecimalFormatTests {
@Test
public void testDecimalFormat(){
double pi = 3.141592653;
System.out.println(new DecimalFormat(".0").format(pi)); // 3.1
System.out.println(new DecimalFormat("0.0").format(pi)); // 3.1
System.out.println(new DecimalFormat("00.0").format(pi)); // 03.1
System.out.println(new DecimalFormat(".#").format(pi)); // 3.1
System.out.println(new DecimalFormat("#.#").format(pi)); // 3.1
System.out.println(new DecimalFormat("##.#").format(pi)); // 3.1
System.out.println(new DecimalFormat(".#").format((int)pi)); // 3.0
System.out.println(new DecimalFormat(".0").format((int)pi)); // 3.0
System.out.println(new DecimalFormat("0.0").format((int)pi)); // 3.0
System.out.println(new DecimalFormat("#.#").format((int)pi)); // 3
}
}到此這篇關(guān)于Java使用hutool實(shí)現(xiàn)文件大小的友好輸出的文章就介紹到這了,更多相關(guān)Java文件大小輸出內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot使用validation-api實(shí)現(xiàn)對(duì)枚舉類(lèi)參數(shù)校驗(yàn)的方法
這篇文章主要介紹了SpringBoot使用validation-api實(shí)現(xiàn)對(duì)枚舉類(lèi)參數(shù)校驗(yàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
spring boot中內(nèi)嵌redis的使用方法示例
這篇文章主要給大家介紹了關(guān)于spring boot中內(nèi)嵌redis使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06
Java代碼為例講解堆的性質(zhì)和基本操作以及排序方法
堆數(shù)據(jù)結(jié)構(gòu)可以看作一顆完全二叉樹(shù),因而又被成為二叉堆,這里我們以Java代碼為例講解堆的性質(zhì)和基本操作以及排序方法,需要的朋友可以參考下2016-06-06
SpringBoot基于AOP的本地/遠(yuǎn)程調(diào)用動(dòng)態(tài)路由實(shí)踐指南
這篇文章主要為大家詳細(xì)介紹了SpringBoot基于AOP的本地/遠(yuǎn)程調(diào)用動(dòng)態(tài)路由實(shí)踐指南,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-07-07
Java實(shí)現(xiàn)限制文件上傳類(lèi)型功能
這篇文章主要為大家詳細(xì)介紹了在Java中如何實(shí)現(xiàn)限制文件上傳類(lèi)型功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-07-07
Springboot全局異常捕獲及try catch區(qū)別解析
這篇文章主要介紹了Springboot全局異常捕獲及try catch區(qū)別解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
創(chuàng)建一個(gè)Java的不可變對(duì)象
這篇文章主要介紹了創(chuàng)建一個(gè)Java的不可變對(duì)象,一個(gè)類(lèi)的對(duì)象在通過(guò)構(gòu)造方法創(chuàng)建后如果狀態(tài)不會(huì)再被改變,那么它就是一個(gè)不可變(immutable)類(lèi)。它的所有成員變量的賦值僅在構(gòu)造方法中完成,不會(huì)提供任何 setter 方法供外部類(lèi)去修改,需要的朋友可以參考下2021-11-11

