/**
* 获取文件夹大小
* @param file File实例
* @return long 单位为M
* @throws Exception
*/
public static long getFolderSize(java.io.File file)throws Exception{
long size = 0;
if(!file.isDirectory()){
size += file.length();
return size;
}
java.io.File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
size = size + getFolderSize(fileList[i]);
} else {
size = size + fileList[i].length();
}
}
return size;
}
/* 得到文件大小*/
@SuppressWarnings("resource")
public static long getFileSizes(File f) throws Exception {
FileInputStream file = new FileInputStream(f);
return file.available();
}
/* 转换文件大小*/
public static String FormetFileSize(long fileS) {
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + "KB";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + "MB";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + "GB";
}
return fileSizeString;
}
获取文件夹大小
最新推荐文章于 2022-08-03 20:06:19 发布