Get folder size

前端 未结 8 1197
生来不讨喜
生来不讨喜 2020-12-30 06:27

Do you know how can I get the folder size in Java?

The length() method in the File class only works for files, using that method I always get a size of 0.

8条回答
  •  悲哀的现实
    2020-12-30 06:48

    Go through a folder, file by file, getting the size of each file and adding them to the variable size.

    static int size=0;
    
     public static int folderSize(File folder){
    
        size = 0;
    
        File[] fileList = folder.listFiles();
    
        for(File file : fileList){
            if(!file.isFile()){ 
                folderSize(file);
            }
            size += file.length();
        }
        return size;
    }
    

提交回复
热议问题