zoukankan      html  css  js  c++  java
  • Java计算机硬盘大小转换(B,KB,MB,GB,TB,PB之间的大小转换)

    程序员都很懒,你懂的!

    java程序员在实际的开发中会遇到很多的单位换算问题。今天我给大家带来的是关于计算机硬盘大小的换算。多数情况下,一般要求 b,kb,mb,gb,tb,pb之间的大小转换,我们都知道他们之间的换算是乘以1024或者除以1024。但是具体怎么用java代码来实现呢?请看 下面的代码:

      1 package com.herman.utils;  
      2    
      3 /*** 
      4  * @see 存储大小(单位)转换器. 
      5  * @author Herman.Xiong 
      6  * @date 2014年5月27日 13:27:40 
      7  * @version V1.0 
      8  */ 
      9 public enum SizeConverter {  
     10     /** 转换任意单位的大小, 返回结果会包含两位小数但不包含单位. */ 
     11     Arbitrary {  
     12         @Override 
     13         public String convert(float size) {  
     14             while (size > 1024) {  
     15                 size /= 1024;  
     16             }  
     17             return String.format(FORMAT_F, size);  
     18         }  
     19     },  
     20        
     21     // -----------------------------------------------------------------------  
     22     // 有单位  
     23     /** 转换单位为B的大小, 返回结果会包含两位小数以及单位. 如: 1024B->1KB, (1024*1024)B->1MB */ 
     24     B {  
     25         @Override 
     26         public String convert(float B) {  
     27             return converter(0, B);  
     28         }  
     29     },  
     30     /** 转换单位为B的大小, 返回结果会包含两位小数以及单位. */ 
     31     KB {  
     32         @Override 
     33         public String convert(float KB) {  
     34             return converter(1, KB);  
     35         }  
     36     },  
     37     /** 转换单位为MB的大小, 返回结果会包含两位小数以及单位. */ 
     38     MB {  
     39         @Override 
     40         public String convert(float MB) {  
     41             return converter(2, MB);  
     42         }  
     43     },  
     44     /** 转换单位为GB的大小, 返回结果会包含两位小数以及单位. */ 
     45     GB {  
     46         @Override 
     47         public String convert(float GB) {  
     48             return converter(3, GB);  
     49         }  
     50     },  
     51     /** 转换单位为TB的大小, 返回结果会包含两位小数以及单位. */ 
     52     TB {  
     53         @Override 
     54         public String convert(float TB) {  
     55             return converter(4, TB);  
     56         }  
     57     },  
     58        
     59     // -----------------------------------------------------------------------  
     60     // trim没单位  
     61     /** 转换任意单位的大小, 返回结果小数部分为0时将去除两位小数, 不包含单位. */ 
     62     ArbitraryTrim {  
     63         @Override 
     64         public String convert(float size) {  
     65             while (size > 1024) {  
     66                 size /= 1024;  
     67             }  
     68    
     69             int sizeInt = (int) size;  
     70             boolean isfloat = size - sizeInt > 0.0F;  
     71             if (isfloat) {  
     72                 return String.format(FORMAT_F, size);  
     73             }  
     74             return String.format(FORMAT_D, sizeInt);  
     75         }  
     76     },  
     77        
     78     // -----------------------------------------------------------------------  
     79     // trim有单位  
     80     /** 转换单位为B的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */ 
     81     BTrim {  
     82         @Override 
     83         public String convert(float B) {  
     84             return trimConverter(0, B);  
     85         }  
     86     },  
     87     /** 转换单位为KB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */ 
     88     KBTrim {  
     89         @Override 
     90         public String convert(float KB) {  
     91             return trimConverter(1, KB);  
     92         }  
     93     },  
     94     /** 转换单位为MB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */ 
     95     MBTrim {  
     96         @Override 
     97         public String convert(float MB) {  
     98             return trimConverter(2, MB);  
     99         }  
    100     },  
    101     /** 转换单位为GB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */ 
    102     GBTrim {  
    103         @Override 
    104         public String convert(float GB) {  
    105             return trimConverter(3, GB);  
    106         }  
    107     },  
    108     /** 转换单位为TB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */ 
    109     TBTrim {  
    110         @Override 
    111         public String convert(float TB) {  
    112             return trimConverter(4, TB);  
    113         }  
    114     };  
    115     /*** 
    116      * <p> 将指定的大小转换到1024范围内的大小. 注意该方法的最大单位为PB, 最小单位为B,  
    117      * 任何超出该范围的单位最终会显示为**. </p> 
    118      *  
    119      * @param size 要转换的大小, 注意是浮点数, 不要以整形的方式传入, 容易造成溢出. 
    120      *         (如: 1024*1024*1024*1024*1024会溢出, 使结果为0, 因为它先将结果以int相乘后再转换为float;  
    121      *         而1024.0F*1024.0F*1024.0F*1024.0F*1024.0F就不会溢出) 
    122      * @return 
    123      */ 
    124     abstract public String convert(float size);  
    125        
    126     // -----------------------------------------------------------------------  
    127     // 单位转换  
    128        
    129     private static final String[] UNITS = new String[] {  
    130         "B", "KB", "MB", "GB", "TB", "PB", "**" 
    131     };  
    132        
    133     private static final int LAST_IDX = UNITS.length-1;  
    134        
    135     private static final String FORMAT_F = "%1$-1.2f";  
    136     private static final String FORMAT_F_UNIT = "%1$-1.2f%2$s";  
    137        
    138     private static final String FORMAT_D = "%1$-1d";  
    139     private static final String FORMAT_D_UNIT = "%1$-1d%2$s";  
    140        
    141     // -----------------------------------------------------------------------  
    142     private static String converter(int unit, float size) {  
    143         int unitIdx = unit;  
    144         while (size > 1024) {  
    145             unitIdx++;  
    146             size /= 1024;  
    147         }  
    148         int idx = unitIdx < LAST_IDX ? unitIdx : LAST_IDX;  
    149         return String.format(FORMAT_F_UNIT, size, UNITS[idx]);  
    150     }  
    151        
    152     private static String trimConverter(int unit, float size) {  
    153         int unitIdx = unit;  
    154         while (size > 1024) {  
    155             unitIdx++;  
    156             size /= 1024;  
    157         }  
    158    
    159         int sizeInt = (int) size;  
    160         boolean isfloat = size - sizeInt > 0.0F;  
    161         int idx = unitIdx < LAST_IDX ? unitIdx : LAST_IDX;  
    162         if (isfloat) {  
    163             return String.format(FORMAT_F_UNIT, size, UNITS[idx]);  
    164         }  
    165         return String.format(FORMAT_D_UNIT, sizeInt, UNITS[idx]);  
    166     }  
    167        
    168     // -----------------------------------------------------------------------  
    169     public static String convertBytes(float B, boolean trim) {  
    170         return trim ? trimConvert(0, B, true) : convert(0, B, true);  
    171     }  
    172        
    173     public static String convertKB(float KB, boolean trim) {  
    174         return trim ? trimConvert(1, KB, true) : convert(1, KB, true);  
    175     }  
    176        
    177     public static String convertMB(float MB, boolean trim) {  
    178         return trim ? trimConvert(2, MB, true) : convert(2, MB, true);  
    179     }  
    180        
    181     /*** 
    182      * <p> 存储大小单位间的转换. 注意该方法的最大单位为PB, 最小单位为B,  
    183      * 任何超出该范围的单位最终会显示为**. </p> 
    184      *  
    185      * @param unit 从哪个单位开始 
    186      * @param size 存储大小, 注意是float, 不要以整形的形式传入, 否则会溢出(如:1024*1024这种, 
    187      * 它是先将1024*1024作为int相乘再转换为float的, 如果值过大的话就会溢出了,  
    188      * 所以这么写1024.0F*1024.0F) 
    189      * @param withUnit 返回的结果字符串是否带有对应的单位 
    190      * @return 
    191      */ 
    192     private static String convert(int unit, float size, boolean withUnit) {  
    193         int unitIdx = unit;  
    194         while (size > 1024) {  
    195             unitIdx++;  
    196             size /= 1024;  
    197         }  
    198         if (withUnit) {  
    199             int idx = unitIdx < LAST_IDX ? unitIdx : LAST_IDX;  
    200             return String.format(FORMAT_F_UNIT, size, UNITS[idx]);  
    201         }  
    202         return String.format(FORMAT_F, size);  
    203     }  
    204        
    205     /*** 
    206      * <p> 存储大小单位间的转换, 如果转换后小数部分为0, 则去除小数部分.  
    207      * 注意该方法的最大单位为PB, 最小单位为B, 任何超出该范围的单位最终会显示为**. </p> 
    208      *  
    209      * @param unit 从哪个单位开始 
    210      * @param size 存储大小, 注意是float, 不要以整形的形式传入, 否则会溢出(如:1024*1024这种, 
    211      * 它是先将1024*1024作为int相乘再转换为float的, 如果值过大的话就会溢出了,  
    212      * 所以这么写1024.0F*1024.0F) 
    213      * @param withUnit 返回的结果字符串是否带有对应的单位 
    214      * @return 
    215      */ 
    216     private static String trimConvert(int unit, float size, boolean withUnit) {  
    217         int unitIdx = unit;  
    218         while (size > 1024) {  
    219             unitIdx++;  
    220             size /= 1024;  
    221         }  
    222    
    223         int sizeInt = (int) size;  
    224         boolean isfloat = size - sizeInt > 0.0F;  
    225         if (withUnit) {  
    226             int idx = unitIdx < LAST_IDX ? unitIdx : LAST_IDX;  
    227             if (isfloat) {  
    228                 return String.format(FORMAT_F_UNIT, size, UNITS[idx]);  
    229             }  
    230             return String.format(FORMAT_D_UNIT, sizeInt, UNITS[idx]);  
    231         }  
    232    
    233         if (isfloat) {  
    234             return String.format(FORMAT_F, size);  
    235         }  
    236         return String.format(FORMAT_D, sizeInt);  
    237     }  
    238 }

    工具类代码写好了,我们来看一个测试类吧,上代码:

     1 package com.herman.test;  
     2    
     3 import com.herman.utils.SizeConverter;  
     4 /** 
     5  * @see 硬盘大小换算测试类 
     6  * @author Herman.Xiong 
     7  * @date 2014年5月27日 13:43:33 
     8  */ 
     9 public class SizeConverterTest {  
    10     public static void main(String[] args) {  
    11         System.out.println(SizeConverter.MBTrim.convert(419562f));  
    12     }  
    13 }
  • 相关阅读:
    IIS: Idle Timeout vs Recycle
    Window.location
    Web technology for developersSee Web APIsStorage
    Know the basics about NTFS permissions
    设置描述性弹性域某个字段为只读
    adb root
    nvme WVLOCK
    模拟器获取root权限
    Android模拟器emulator基本使用技巧和命令
    人工智能可以产生自主意识吗
  • 原文地址:https://www.cnblogs.com/lr393993507/p/5543017.html
Copyright © 2011-2022 走看看