Java使用hutool實現文件大小的友好輸出
更新時間:2023年11月30日 14:04:27 作者:彭世瑜
這篇文章主要為大家詳細介紹了Java如何使用hutool實現文件大小的友好輸出,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解下
文檔
基本使用
依賴
<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 } }
代碼實現
看下他的實現方式
package cn.hutool.core.io.unit; import java.text.DecimalFormat; /** * 數據大小工具類 * * @author looly * @since 5.3.10 */ public class DataSizeUtil { /** * 解析數據大小字符串,轉換為bytes大小 * * @param text 數據大小字符串,類似于: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類型大小 * @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為底的對數,代碼很簡潔
自定義實現代碼
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]); } }
補充知識
換底公式
loga?b=logc?b÷logc?a
DecimalFormat
DecimalFormat 用于數字格式化
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 } }
到此這篇關于Java使用hutool實現文件大小的友好輸出的文章就介紹到這了,更多相關Java文件大小輸出內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java中java.lang.ClassCastException異常原因以及解決方法詳解
這篇文章主要給大家介紹了關于Java中java.lang.ClassCastException異常原因以及解決方法的相關資料,ClassCastException從字面上看是類型轉換錯誤,通常是進行強制類型轉換時候出的錯誤,需要的朋友可以參考下2024-02-02