找回密碼
 註冊
搜索
查看: 1659|回復: 0

[教學] Android 如何獲取資料夾下的所有檔案大小總和

[複製鏈接]
發表於 2020-1-15 12:40:14 | 顯示全部樓層 |閱讀模式
 
Push to Facebook

由於地圖檔OBB檔案時在太多與太大
只好分段處理,來降低負載資源
所以會用到下面程式碼了


  1. public class FilesPath extends AppCompatActivity {
  2. public static long getFileFolderSize(File dir) {
  3.         long size = 0;
  4.         if (dir.isDirectory()) {
  5.             for (File file : dir.listFiles()) {
  6.                 if (file.isFile()) {
  7.                     size += file.length();
  8.                 } else
  9.                     size += getFileFolderSize(file);
  10.             }
  11.         } else if (dir.isFile()) {
  12.             size += dir.length();
  13.         }
  14.         return size;
  15.     }
  16. }
複製代碼

到main呼叫即可
  1. FilesPath FP = new FilesPath();
  2. File file = new File(filesname);
  3. size = FP.getFileSize(file);
複製代碼




 
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

Archiver|手機版|小黑屋|TShopping

GMT+8, 2025-4-30 19:52 , Processed in 0.033255 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回復 返回頂部 返回列表