开发者

java get folder size of multiple directories with file[]

开发者 https://www.devze.com 2023-01-26 02:58 出处:网络
I have some code ive found online and tried to adapt to look through multiple folder chosen via fileChooser

I have some code ive found online and tried to adapt to look through multiple folder chosen via fileChooser

public long getFolderSize(File[] selectedDirectories) {
 long foldersize = 0;

 for(int i = 0; i < selectedDirectories.length; i++){
  File[] currentFolder = selectedDirectories[i].listFiles();

  for (int q = 0; q < currentFolder.length; q++) {
   if (currentFold开发者_JAVA百科er[q].isDirectory()) {
    //if folder run self on q'th folder - in which case the files.length will be counted for the files inside
    foldersize += getFolderSize(currentFolder[q]);//<<the error is here 
   } else {
    //else get file size
    foldersize += currentFolder[q].length();
   }
  }
 return foldersize;
 }

}

The error is at:

getFolderSize(currentFolder[q]) 

Because im implying that its using File and not File[] but im stuck on how to fix it


Just change the signature from array to varargs:

public static long getgFolderSize(final File... selectedDirectories){
    long foldersize = 0;
    for(final File item : selectedDirectories){
        for(final File subItem : item.listFiles()){
            if(subItem.isDirectory()){
                foldersize += getFolderSize(subItem);
            } else{
                foldersize += subItem.length();
            }
        }
    }
    return foldersize;
}

Now you can call the method either with one or more Files or with an array of files.

Test code: (Updated so you can see that both it works identically whether using varargs or not, will fail if your home directory has less than 5 sub folders).

public static void main(final String[] args) throws Exception{
    final File homeFolder = new File(System.getProperty("user.home"));
    final File[] subFolders = homeFolder.listFiles(new FileFilter(){

        private int ct = 0;

        @Override
        public boolean accept(final File pathname){
            return pathname.isDirectory() && ct++ < 5;
        }
    });
    System.out.println("Folders to check:" + Arrays.toString(subFolders));
    long accumulated = 0l;

    for(final File file : subFolders){
        accumulated += getFolderSize(file);
    }
    final long allAtOnce = getFolderSize(subFolders);
    final long withVarArgs =
        getFolderSize(subFolders[0], subFolders[1], subFolders[2],
            subFolders[3], subFolders[4]);
    System.out.println("Accumulated: " + accumulated);
    System.out.println("All at once: " + allAtOnce);
    System.out.println("With varargs: " + withVarArgs);
}

(Calculates the size of the first 5 folders in your home dir, should work on all platforms, fails with an ArrayIndexOutOfBoundException if there are less than five folders in your home dir).

Output on my machine:

Folders to check:[/home/seanizer/Ubuntu One, /home/seanizer/Documents, /home/seanizer/.java, /home/seanizer/.mozilla, /home/seanizer/.evolution]
Accumulated: 1245886955
All at once: 1245886955
With varargs: 1245886955


You can create a File[] of length one and just add the File object to it, then pass that for the recursive call. That will fix the error you are seeing.

File[] tempArray = new File[1]; 
tempArray[0] = currentFolder[q]; 
foldersize += getFolderSize(tempArray); 


Generally when I write my methods I like to separate them out into specific tasks. So in this case what I would have done is write the method:

public long getFolderSize(File directory) {
    long foldersize = 0;

    File[] currentFolder = directory.listFiles();

    for (int q = 0; q < currentFolder.length; q++) {
        if (currentFolder[q].isDirectory()) {
            //if folder run self on q'th folder - in which case the files.length will be counted for the files inside
            foldersize += getFolderSize(currentFolder[q]); 
        } else {
            //else get file size
            foldersize += currentFolder[q].length();
        }
    }
    return foldersize;
}

Then I would write another method for handling a collection of directories:

public long getFolderSize(File[] selectedDirectories) {
    long foldersize = 0;

    for(int i = 0; i < selectedDirectories.length; i++){
        folderSize += getFolderSize(selectedDirectories[i]);
    }
    return foldersize;
}

This way you can easily re-use the methods depending on the situation (i.e. single directory or collection) without having to create an array for example just to put a single directory in.

Alternatively, you could keep the single method that takes the File array and do the recursion as follows:

public long getFolderSize(File[] directoryList) {
    long folderSize = 0;
    for (int i = 0; i < directoryList.length; i++) {
        File currentFile = directoryList[i];
        if (currentFile.isDirectory()) {
            folderSize += getFolderSize(currentFile.listFiles());
        } else {
            folderSize += currentFile.length();
        }
    }
    return folderSize;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消