java用文件[]获取多个目录的文件夹大小

时间:2010-11-19 14:02:46

标签: java file loops

我在网上发现了一些代码,并尝试通过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 (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]);//<<the error is here 
   } else {
    //else get file size
    foldersize += currentFolder[q].length();
   }
  }
 return foldersize;
 }

}

错误发生在:

getFolderSize(currentFolder[q]) 

因为我暗示它使用文件而不是文件[],但我坚持如何解决它

3 个答案:

答案 0 :(得分:0)

您可以创建长度为1的File [],只需将File对象添加到其中,然后将其传递给递归调用。这将解决您所看到的错误。

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

答案 1 :(得分:0)

只需将签名从数组更改为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;
}

现在,您可以使用一个或多个文件或文件数组调用该方法。

测试代码:(已更新,因此您可以看到无论是否使用varargs,它们的工作方式都相同,如果您的主目录少于5个子文件夹,则会失败。)

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);
}

(计算家庭目录中前5个文件夹的大小,应该适用于所有平台,如果家庭目录中的文件夹少于5个,则失败并显示ArrayIndexOutOfBoundException

我机器上的输出:

  

要检查的文件夹:[/ home / seanizer / Ubuntu One,/ home / seanizer / Documents,/ home / theseizer / .java,/ home / seanizer / .mozilla,/ home / seanizer /。evolution]
  累计:1245886955
  一下子:1245886955
  随着varargs:1245886955

答案 2 :(得分:0)

通常,当我编写方法时,我喜欢将它们分成特定的任务。所以在这种情况下我要做的就是编写方法:

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;
}

然后我会编写另一种处理目录集合的方法:

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

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

通过这种方式,您可以根据具体情况(即单个目录或集合)轻松地重复使用这些方法,而无需创建数组,例如只是为了放入一个目录。

或者,您可以保留采​​用File数组的单个方法并按如下方式执行递归:

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;
}