[英]java get folder size of multiple directories with file[]
I have some code ive found online and tried to adapt to look through multiple folder chosen via fileChooser 我在网上找到了一些代码,并且试图适应通过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;
}
} }
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 因为我暗示它使用的是File而不是File [],但是我坚持如何修复它
You can create a File[] of length one and just add the File object to it, then pass that for the recursive call. 您可以创建一个长度为1的File []并将其添加到File对象,然后将其传递给递归调用。 That will fix the error you are seeing.
这样可以解决您所看到的错误。
File[] tempArray = new File[1];
tempArray[0] = currentFolder[q];
foldersize += getFolderSize(tempArray);
Just change the signature from array to varargs: 只需将签名从数组更改为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). 测试代码:(已更新,因此您可以看到,无论您的主目录中的子文件夹少于5个,无论是否使用varargs,这两种方法都可以正常工作)。
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). (计算主目录中前五个文件夹的大小,应该在所有平台上都可以使用,如果主目录中的文件夹少于五个,则失败,并出现
ArrayIndexOutOfBoundException
)。
Output on my machine: 我的机器上的输出:
Folders to check:[/home/seanizer/Ubuntu One, /home/seanizer/Documents, /home/seanizer/.java, /home/seanizer/.mozilla, /home/seanizer/.evolution]
要检查的文件夹:[/ home / seanizer / Ubuntu One,/ home / seanizer / Documents,/ home / seanizer / .java,/ home / seanizer / .mozilla,/ home / seanizer / .evolution]
Accumulated: 1245886955累计:1245886955
All at once: 1245886955一次全部:1245886955
With varargs: 1245886955带有可变参数:1245886955
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 (ie 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: 或者,您可以保留采用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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.