[java]
/**
* 獲取文件夾大小
* @param file File實例
* @return long 單位為M
* @throws Exception
*/
public static long getFolderSize(java.io.File file)throws Exception{
long size = 0;
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/1048576;
}
[java]
/**
* 刪除指定目錄下文件及目錄
*
* @param deleteThisPath
* @param filepath
* @return
*/
public void deleteFolderFile(String filePath, boolean deleteThisPath)
throws IOException {
if (!TextUtils.isEmpty(filePath)) {
File file = new File(filePath);
if (file.isDirectory()) {// 處理目錄
File files[] = file.listFiles();
for (int i = 0; i < files.length; i++) {
deleteFolderFile(files[i].getAbsolutePath(), true);
} www.2cto.com
}
if (deleteThisPath) {
if (!file.isDirectory()) {// 如果是文件,刪除
file.delete();
} else {// 目錄
if (file.listFiles().length == 0) {// 目錄下沒有文件或者目錄,刪除
file.delete();
}
}
}
}
}