获取文件大小
public static String getSize(File file) {
double d;
DecimalFormat df = new DecimalFormat("#.00");
long length = file.length();
if (1024L <= length && length < 1024 * 1024L) {
d = length / 1024.0F;
return df.format(d) + "KB";
} else if (1024 * 1024L <= length && length < 1024 * 1024 * 1024L) {
d = length / (1024 * 1024.0F);
return df.format(d) + "MB";
} else if (1024 * 1024 * 1024L <= length && length < 1024 * 1024 * 1024 * 1024L) {
d = length / (1024 * 1024 * 1024.0F);
return df.format(d) + "GB";
}
return length + "B";
}
测试结果
源文件大小