packagecom.example.tt.utils;
importandroid.util.Log;
importjava.io.File;
importjava.io.FileInputStream;
importjava.text.DecimalFormat;
/**
* Created by Administrator on 2017/11/20.
*/
public classFileSizeUtil {
public static final intSIZETYPE_B=1;//获取文件大小单位为B的double值
public static final intSIZETYPE_KB=2;//获取文件大小单位为KB的double值
public static final intSIZETYPE_MB=3;//获取文件大小单位为MB的double值
public static final intSIZETYPE_GB=4;//获取文件大小单位为GB的double值
/**
* 获取文件指定文件的指定单位的大小
*@paramfilePath文件路径
*@paramsizeType获取大小的类型1为B、2为KB、3为MB、4为GB
*@returndouble值的大小
*/
public static doublegetFileOrFilesSize(String filePath,intsizeType){
File file=newFile(filePath);
longblockSize=0;
try{
if(file.isDirectory()){
blockSize =getFileSizes(file);
}else{
blockSize =getFileSize(file);
}
}catch(Exception e) {
e.printStackTrace();
Log.e("获取文件大小","获取失败!");
}
returnFormetFileSize(blockSize, sizeType);
}
/**
* 调用此方法自动计算指定文件或指定文件夹的大小
*@paramfilePath文件路径
*@return计算好的带B、KB、MB、GB的字符串
*/
public staticString getAutoFileOrFilesSize(String filePath){
File file=newFile(filePath);
longblockSize=0;
try{
if(file.isDirectory()){
blockSize =getFileSizes(file);
}else{
blockSize =getFileSize(file);
}
}catch(Exception e) {
e.printStackTrace();
Log.e("获取文件大小","获取失败!");
}
returnFormetFileSize(blockSize);
}
/**
* 获取指定文件大小
*@return
*@throwsException
*/
private static longgetFileSize(File file)throwsException
{
longsize =0;
if(file.exists()){
FileInputStream fis =null;
fis =newFileInputStream(file);
size = fis.available();
}
else{
file.createNewFile();
Log.e("获取文件大小","文件不存在!");
}
returnsize;
}
/**
* 获取指定文件夹
*@paramf
*@return
*@throwsException
*/
private static longgetFileSizes(File f)throwsException
{
longsize =0;
File flist[] = f.listFiles();
for(inti =0; i < flist.length; i++){
if(flist[i].isDirectory()){
size = size +getFileSizes(flist[i]);
}
else{
size =size +getFileSize(flist[i]);
}
}
returnsize;
}
/**
* 转换文件大小
*@paramfileS
*@return
*/
private staticString FormetFileSize(longfileS)
{
DecimalFormat df =newDecimalFormat("#.00");
String fileSizeString ="";
String wrongSize="0B";
if(fileS==0){
returnwrongSize;
}
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";
}
returnfileSizeString;
}
/**
* 转换文件大小,指定转换的类型
*@paramfileS
*@paramsizeType
*@return
*/
private static doubleFormetFileSize(longfileS,intsizeType)
{
DecimalFormat df =newDecimalFormat("#.00");
doublefileSizeLong =0;
switch(sizeType) {
caseSIZETYPE_B:
fileSizeLong=Double.valueOf(df.format((double) fileS));
break;
caseSIZETYPE_KB:
fileSizeLong=Double.valueOf(df.format((double) fileS /1024));
break;
caseSIZETYPE_MB:
fileSizeLong=Double.valueOf(df.format((double) fileS /1048576));
break;
caseSIZETYPE_GB:
fileSizeLong=Double.valueOf(df.format((double) fileS /1073741824));
break;
default:
break;
}
returnfileSizeLong;
}
}
网友评论