android 獲取文件夾、文件的大小 以B、KB、MB、GB 為單位
public class FileSizeUtil {
public static final int SIZETYPE_B = 1;//獲取文件大小單位為B的double值
public static final int SIZETYPE_KB = 2;//獲取文件大小單位為KB的double值
public static final int SIZETYPE_MB = 3;//獲取文件大小單位為MB的double值
public static final int SIZETYPE_GB = 4;//獲取文件大小單位為GB的double值
/**
* 獲取文件指定文件的指定單位的大小
* @param filePath 文件路徑
* @param sizeType 獲取大小的類型1為B、2為KB、3為MB、4為GB
* @return double值的大小
*/
public static double getFileOrFilesSize(String filePath,int sizeType){
File file=new File(filePath);
long blockSize=0;
try {
if(file.isDirectory()){
blockSize = getFileSizes(file);
}else{
blockSize = getFileSize(file);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("獲取文件大小","獲取失敗!");
}
return FormetFileSize(blockSize, sizeType);
}
/**
* 調用此方法自動計算指定文件或指定文件夾的大小
* @param filePath 文件路徑
* @return 計算好的帶B、KB、MB、GB的字符串
*/
public static String getAutoFileOrFilesSize(String filePath){
File file=new File(filePath);
long blockSize=0;
try {
if(file.isDirectory()){
blockSize = getFileSizes(file);
}else{
blockSize = getFileSize(file);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("獲取文件大小","獲取失敗!");
}
return FormetFileSize(blockSize);
}
/**
* 獲取指定文件大小
* @param f
* @return
* @throws Exception
*/
private static long getFileSize(File file) throws Exception
{
long size = 0;
if (file.exists()){
FileInputStream fis = null;
fis = new FileInputStream(file);
size = fis.available();
}
else{
file.createNewFile();
Log.e("獲取文件大小","文件不存在!");
}
return size;
}
/**
* 獲取指定文件夾
* @param f
* @return
* @throws Exception
*/
private static long getFileSizes(File f) throws Exception
{
long size = 0;
File flist[] = f.listFiles();
for (int i = 0; i < flist.length; i++){
if (flist[i].isDirectory()){
size = size + getFileSizes(flist[i]);
}
else{
size =size + getFileSize(flist[i]);
}
}
return size;
}
/**
* 轉換文件大小
* @param fileS
* @return
*/
private static String FormetFileSize(long fileS)
{
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
String wrongSize="0B";
if(fileS==0){
return wrongSize;
}
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;
}
/**
* 轉換文件大小,指定轉換的類型
* @param fileS
* @param sizeType
* @return
*/
private static double FormetFileSize(long fileS,int sizeType)
{
DecimalFormat df = new DecimalFormat("#.00");
double fileSizeLong = 0;
switch (sizeType) {
case SIZETYPE_B:
fileSizeLong=Double.valueOf(df.format((double) fileS));
break;
case SIZETYPE_KB:
fileSizeLong=Double.valueOf(df.format((double) fileS / 1024));
break;
case SIZETYPE_MB:
fileSizeLong=Double.valueOf(df.format((double) fileS / 1048576));
break;
case SIZETYPE_GB:
fileSizeLong=Double.valueOf(df.format((double) fileS / 1073741824));
break;
default:
break;
}
return fileSizeLong;
}