小伙伴们好,欢迎关注,一起学习,无限进步
在程序开发的过程中,文件的大小在视图呈现和数据库存储的过程不一致怎么转换呢
文件大小的单位,在计算机中,文件大小通常使用字节(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;
}
}