Java计算机硬盘大小转换(B,KB,MB,GB,TB,PB之间的大小转换)
程序员都很懒,你懂的!
java程序员在实际的开发中会遇到很多的单位换算问题。今天我给大家带来的是关于计算机硬盘大小的换算。多数情况下,一般要求 b,kb,mb,gb,tb,pb之间的大小转换,我们都知道他们之间的换算是乘以1024或者除以1024。但是具体怎么用java代码来实现呢?请看 下面的代码:
package com.herman.utils; /***
* @see 存储大小(单位)转换器.
* @author Herman.Xiong
* @date 2014年5月27日 13:27:40
* @version V1.0
*/
public enum SizeConverter {
/** 转换任意单位的大小, 返回结果会包含两位小数但不包含单位. */
Arbitrary {
@Override
public String convert(float size) {
while (size > 1024) {
size /= 1024;
}
return String.format(FORMAT_F, size);
}
}, // -----------------------------------------------------------------------
// 有单位
/** 转换单位为B的大小, 返回结果会包含两位小数以及单位. 如: 1024B->1KB, (1024*1024)B->1MB */
B {
@Override
public String convert(float B) {
return converter(0, B);
}
},
/** 转换单位为B的大小, 返回结果会包含两位小数以及单位. */
KB {
@Override
public String convert(float KB) {
return converter(1, KB);
}
},
/** 转换单位为MB的大小, 返回结果会包含两位小数以及单位. */
MB {
@Override
public String convert(float MB) {
return converter(2, MB);
}
},
/** 转换单位为GB的大小, 返回结果会包含两位小数以及单位. */
GB {
@Override
public String convert(float GB) {
return converter(3, GB);
}
},
/** 转换单位为TB的大小, 返回结果会包含两位小数以及单位. */
TB {
@Override
public String convert(float TB) {
return converter(4, TB);
}
}, // -----------------------------------------------------------------------
// trim没单位
/** 转换任意单位的大小, 返回结果小数部分为0时将去除两位小数, 不包含单位. */
ArbitraryTrim {
@Override
public String convert(float size) {
while (size > 1024) {
size /= 1024;
} int sizeInt = (int) size;
boolean isfloat = size - sizeInt > 0.0F;
if (isfloat) {
return String.format(FORMAT_F, size);
}
return String.format(FORMAT_D, sizeInt);
}
}, // -----------------------------------------------------------------------
// trim有单位
/** 转换单位为B的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */
BTrim {
@Override
public String convert(float B) {
return trimConverter(0, B);
}
},
/** 转换单位为KB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */
KBTrim {
@Override
public String convert(float KB) {
return trimConverter(1, KB);
}
},
/** 转换单位为MB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */
MBTrim {
@Override
public String convert(float MB) {
return trimConverter(2, MB);
}
},
/** 转换单位为GB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */
GBTrim {
@Override
public String convert(float GB) {
return trimConverter(3, GB);
}
},
/** 转换单位为TB的大小, 返回结果小数部分为0时将去除两位小数, 会包含单位. */
TBTrim {
@Override
public String convert(float TB) {
return trimConverter(4, TB);
}
};
/***
* <p> 将指定的大小转换到1024范围内的大小. 注意该方法的最大单位为PB, 最小单位为B,
* 任何超出该范围的单位最终会显示为**. </p>
*
* @param size 要转换的大小, 注意是浮点数, 不要以整形的方式传入, 容易造成溢出.
* (如: 1024*1024*1024*1024*1024会溢出, 使结果为0, 因为它先将结果以int相乘后再转换为float;
* 而1024.0F*1024.0F*1024.0F*1024.0F*1024.0F就不会溢出)
* @return
*/
abstract public String convert(float size); // -----------------------------------------------------------------------
// 单位转换 private static final String[] UNITS = new String[] {
"B", "KB", "MB", "GB", "TB", "PB", "**"
}; private static final int LAST_IDX = UNITS.length-1; private static final String FORMAT_F = "%1$-1.2f";
private static final String FORMAT_F_UNIT = "%1$-1.2f%2$s"; private static final String FORMAT_D = "%1$-1d";
private static final String FORMAT_D_UNIT = "%1$-1d%2$s"; // -----------------------------------------------------------------------
private static String converter(int unit, float size) {
int unitIdx = unit;
while (size > 1024) {
unitIdx++;
size /= 1024;
}
int idx = unitIdx < LAST_IDX ? unitIdx : LAST_IDX;
return String.format(FORMAT_F_UNIT, size, UNITS[idx]);
} private static String trimConverter(int unit, float size) {
int unitIdx = unit;
while (size > 1024) {
unitIdx++;
size /= 1024;
} int sizeInt = (int) size;
boolean isfloat = size - sizeInt > 0.0F;
int idx = unitIdx < LAST_IDX ? unitIdx : LAST_IDX;
if (isfloat) {
return String.format(FORMAT_F_UNIT, size, UNITS[idx]);
}
return String.format(FORMAT_D_UNIT, sizeInt, UNITS[idx]);
} // -----------------------------------------------------------------------
public static String convertBytes(float B, boolean trim) {
return trim ? trimConvert(0, B, true) : convert(0, B, true);
} public static String convertKB(float KB, boolean trim) {
return trim ? trimConvert(1, KB, true) : convert(1, KB, true);
} public static String convertMB(float MB, boolean trim) {
return trim ? trimConvert(2, MB, true) : convert(2, MB, true);
} /***
* <p> 存储大小单位间的转换. 注意该方法的最大单位为PB, 最小单位为B,
* 任何超出该范围的单位最终会显示为**. </p>
*
* @param unit 从哪个单位开始
* @param size 存储大小, 注意是float, 不要以整形的形式传入, 否则会溢出(如:1024*1024这种,
* 它是先将1024*1024作为int相乘再转换为float的, 如果值过大的话就会溢出了,
* 所以这么写1024.0F*1024.0F)
* @param withUnit 返回的结果字符串是否带有对应的单位
* @return
*/
private static String convert(int unit, float size, boolean withUnit) {
int unitIdx = unit;
while (size > 1024) {
unitIdx++;
size /= 1024;
}
if (withUnit) {
int idx = unitIdx < LAST_IDX ? unitIdx : LAST_IDX;
return String.format(FORMAT_F_UNIT, size, UNITS[idx]);
}
return String.format(FORMAT_F, size);
} /***
* <p> 存储大小单位间的转换, 如果转换后小数部分为0, 则去除小数部分.
* 注意该方法的最大单位为PB, 最小单位为B, 任何超出该范围的单位最终会显示为**. </p>
*
* @param unit 从哪个单位开始
* @param size 存储大小, 注意是float, 不要以整形的形式传入, 否则会溢出(如:1024*1024这种,
* 它是先将1024*1024作为int相乘再转换为float的, 如果值过大的话就会溢出了,
* 所以这么写1024.0F*1024.0F)
* @param withUnit 返回的结果字符串是否带有对应的单位
* @return
*/
private static String trimConvert(int unit, float size, boolean withUnit) {
int unitIdx = unit;
while (size > 1024) {
unitIdx++;
size /= 1024;
} int sizeInt = (int) size;
boolean isfloat = size - sizeInt > 0.0F;
if (withUnit) {
int idx = unitIdx < LAST_IDX ? unitIdx : LAST_IDX;
if (isfloat) {
return String.format(FORMAT_F_UNIT, size, UNITS[idx]);
}
return String.format(FORMAT_D_UNIT, sizeInt, UNITS[idx]);
} if (isfloat) {
return String.format(FORMAT_F, size);
}
return String.format(FORMAT_D, sizeInt);
}
}
工具类代码写好了,我们来看一个测试类吧,上代码:
package com.herman.test; import com.herman.utils.SizeConverter;
/**
* @see 硬盘大小换算测试类
* @author Herman.Xiong
* @date 2014年5月27日 13:43:33
*/
public class SizeConverterTest {
public static void main(String[] args) {
System.out.println(SizeConverter.MBTrim.convert(419562f));
}
}
Java计算机硬盘大小转换(B,KB,MB,GB,TB,PB之间的大小转换)的更多相关文章
- 计算机硬盘大小转换(B,KB,MB,GB,TB,PB之间的大小转换)
程序猿都非常懒.你懂的! java程序猿在实际的开发中会遇到非常多的单位换算问题.今天我给大家带来的是关于计算机硬盘大小的换算.多数情况下.一般要求b,kb,mb,gb,tb,pb之间的大小转换,我们 ...
- 我的Android进阶之旅------>Java文件大小转换工具类 (B,KB,MB,GB,TB,PB之间的大小转换)
Java文件大小转换工具类 (B,KB,MB,GB,TB,PB之间的大小转换) 有时候要做出如下所示的展示文件大小的效果时候,需要对文件大小进行转换,然后再进行相关的代码逻辑编写. 下面是一个Java ...
- java B转换KB MB GB TB PB EB ZB
public static String readableFileSize(long size) { if (size <= 0) { return "0"; } final ...
- 计算机存储单位KB,MB,GB,TB,PB,EB,ZB,YB后面是什么?
关于计算机存储单位KB,MB,GB,TB,PB,EB,ZB,YB后面是什么? 我们知道Kb是1000的1次方,所以 MB就是1000的2次方(106) GB就是1000的3次方(109) TB就是10 ...
- Bit,Bytes,KB,MB,GB,TB,PB,EB,ZB,YB
Bit,Bytes,KB,MB,GB,TB,PB,EB,ZB,YB 汉字字符 2字节英文字符 1字节中文标点 2字节英文标点 1字节一个字节就是一个八位二进制数啊,2就是00000010,4就是000 ...
- 字节b换算kb/mb/gb/tb/pb
public static string HumanReadableFilesize(double size) { string[] units = new string[] { "B&qu ...
- 计算机 KB,MB,GB,TB,PB,EB 计算
ASCII码:一个英文字母(不分大小写)占一个字节的空间.一个二进制数字序列,在计算机中作为一个数字单元,一般为8位二进制数.换算为十进制,最小值-128,最大值127.如一个ASCII码就是一个字节 ...
- b,B,KB,MB,GB,TB,PB,EB,ZB,YB,BB,NB,DB的含义,之间的关系
1bit=1位2进制信息 1B (byte 字节)1KB(Kilobyte 千字节)=2(10)B=1024B=2(10)B: 1MB(Megabyte 兆字节)=2(10)KB=1024KB=2(2 ...
- JS数据容量单位转换(kb,mb,gb,tb)
JS代码如下: var size = '8164674'; function bytesToSize(bytes) { if (bytes === 0) return '0 B'; var k = 1 ...
随机推荐
- 没有调用PageHelper.startPage()分页方法,最后还是执行了PageHelper分页方法的原因
SELECT * FROM ( SELECT TMP_PAGE.*, ROWNUM ROW_ID FROM ( SELECT * FROM ( SELECT A.*, ROWNUM RN FROM ( ...
- 权限验证AuthorizeAttribute
/// <summary> /// 权限验证属性. /// </summary> public class AuthorizeExAttribute : AuthorizeAt ...
- python gensim的第一次试用
参考于 http://blog.csdn.net/xiaoquantouer/article/details/53583980 有一个地方很重要,一定要安装anaconda,安装库简直不要太方便. 先 ...
- centos 安装 redis
1. 下载地址:$ wget http://download.redis.io/releases/redis-3.0.3.tar.gz 2. 解压缩$ tar -zvxf redis-3.0.3.ta ...
- linux下使用crontab创建定时任务
在linux中启动crontab服务: /etc/init.d/crond start crontab的命令格式 crontab -l 显示当前的crontab 文件(默认编写的crontab文件会保 ...
- Mysql配置文件my.ini详解
以下是Mysql数据库服务器配置文件my.ini的详细配置.应用场合是InnoDB引擎,2核CPU, 32位SUSE. [client] #password = your_password port ...
- SourceInsight中文字体
转载自: http://blog.chinaunix.net/uid-29094179-id-3889999.html 1.正确显示中文注释 1)Options->Style Propertie ...
- pytest文档4-测试用例setup和teardown
前言 学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次. 当然还有更高级一点的setupClass和teardownClass,需 ...
- POJ 1737 Connected Graph 题解(未完成)
Connected Graph Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3156 Accepted: 1533 D ...
- @Redis Desktop Manager无法连接虚拟机中启动的redis服务问题解决
Redis Desktop Manager下载地址 https://redisdesktop.com/download 安装好以后连接linux服务器上的Redis服务器错误: 问题排查: 1.检查R ...