【Android筆記】Android統計檔案夾大小,刪除檔案夾下的所有檔案方法

來源:互聯網
上載者:User

標籤:

1、擷取檔案夾大小:

 1 /**    2      * 擷取檔案夾大小    3      * @param file File執行個體    4      * @return long       5      */      6     public static long getFolderSize(java.io.File file){     7     8         long size = 0;     9         try {  10             java.io.File[] fileList = file.listFiles();     11             for (int i = 0; i < fileList.length; i++)     12             {     13                 if (fileList[i].isDirectory())     14                 {     15                     size = size + getFolderSize(fileList[i]);    16    17                 }else{     18                     size = size + fileList[i].length();    19    20                 }     21             }  22         } catch (Exception e) {  23             // TODO Auto-generated catch block  24             e.printStackTrace();  25         }     26        //return size/1048576;    27         return size;    28     }  

2、刪除檔案夾下所有檔案:

 1 /**    2      * 刪除指定目錄下檔案及目錄     3      * @param deleteThisPath    4      * @param filepath    5      * @return     6      */      7     public void deleteFolderFile(String filePath, boolean deleteThisPath) {      8         if (!TextUtils.isEmpty(filePath)) {      9             try {  10                 File file = new File(filePath);     11                 if (file.isDirectory()) {// 處理目錄     12                     File files[] = file.listFiles();     13                     for (int i = 0; i < files.length; i++) {     14                         deleteFolderFile(files[i].getAbsolutePath(), true);     15                     }      16                 }     17                 if (deleteThisPath) {     18                     if (!file.isDirectory()) {// 如果是檔案,刪除     19                         file.delete();     20                     } else {// 目錄     21                    if (file.listFiles().length == 0) {// 目錄下沒有檔案或者目錄,刪除     22                             file.delete();     23                         }     24                     }     25                 }  26             } catch (Exception e) {  27                 // TODO Auto-generated catch block  28                 e.printStackTrace();  29             }     30         }     31     }    

3、格式檔案夾大小單位:

 1 /**  2      * 格式化單位  3      * @param size  4      * @return  5      */   6     public static String getFormatSize(double size) {   7         double kiloByte = size/1024;   8         if(kiloByte < 1) {   9             return size + "B";  10         }  11           12         double megaByte = kiloByte/1024;  13         if(megaByte < 1) {  14             BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));  15             return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";  16         }  17           18         double gigaByte = megaByte/1024;  19         if(gigaByte < 1) {  20             BigDecimal result2  = new BigDecimal(Double.toString(megaByte));  21             return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";  22         }  23           24         double teraBytes = gigaByte/1024;  25         if(teraBytes < 1) {  26             BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));  27             return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";  28         }  29         BigDecimal result4 = new BigDecimal(teraBytes);  30         return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";  31     }  

 

【Android筆記】Android統計檔案夾大小,刪除檔案夾下的所有檔案方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.