/**
*
* @author esle
*
*/
public class FileUtis {
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) + "K";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + "M";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + "G";
}
return fileSizeString;
}
}
public long getFileSize(File file) throws Exception{//文件大小
long size=0;
if (file.exists()) {
FileInputStream fis = null;
fis = new FileInputStream(f);
size= fis.available();
} else {
file.createNewFile();
System.out.println("文件不存在");
}
return size;
}
public long getFileSize(File folder)throws Exception//取得文件夹大小
{
long size = 0;
File flist[] = folder.listFiles();
for (int i = 0; i < flist.length; i++)
{
if (flist[i].isDirectory())
{
size = size + getFileSize(flist[i]);
} else
{
size = size + flist[i].length();
}
}
return size;
}
public long getlist(File f){//递归求取目录文件个数
long size = 0;
File flist[] = f.listFiles();
size=flist.length;
for (int i = 0; i < flist.length; i++) {
if (flist[i].isDirectory()) {
size = size + getlist(flist[i]);
size--;
}
}
return size;
}
}
计算时间差
long startTime= System.currentTimeMillis();
long endTime = System.currentTimeMillis();
System.out.println("总共花费时间为:" + (endTime - startTime) + "毫秒...");