您的位置:首页 > 编程语言 > Java开发

java file 获取文件大小 返回 xx BT xx KB xx MB

2017-09-26 10:05 260 查看
public static String GetFileSize(String Path){
return GetFileSize(new File(Path));
}

public static String GetFileSize(File file){
String size = "";
if(file.exists() && file.isFile()){
long fileS = file.length();
DecimalFormat df = new DecimalFormat("#.00");
if (fileS < 1024) {
size = df.format((double) fileS) + "BT";
} else if (fileS < 1048576) {
size = df.format((double) fileS / 1024) + "KB";
} else if (fileS < 1073741824) {
size = df.format((double) fileS / 1048576) + "MB";
} else {
size = df.format((double) fileS / 1073741824) +"GB";
}
}else if(file.exists() && file.isDirectory()){
size = "";
}else{
size = "0BT";
}
return size;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: