编写一个程序,指定一个文件夹,能自动计算出其总容量

package filetest;
import java.io.File;
import java.io.IOException;

public class FileEdit {
double size=0.0;
//计算文件或文件夹的大小,单位MB
public double getSize(File file){
//判断文件是否存在
if(file.exists()) {
if(!file.isFile()) {
//获取文件大小
File[] fl = file.listFiles();
double ss=0;
for(File f : fl)
ss += getSize(f);
return ss;
}else {
double ss = (double) file.length()/1024/1024;
System.out.println(file.getName()+":"+ss+"MB");
return ss;
}
}else {
System.out.println("文件或文件夹不存在,请检查文件路径是否正确!");
return 0.0;
}
}
public static void main(String[] args) throws IOException{
FileEdit fd = new FileEdit();
double all = fd.getSize(new File("C:\\Users\\FuHeishi826\\Desktop\\壹青年"));
System.out.println("All: "+all+"MB");
}
}

猜你喜欢

转载自www.cnblogs.com/fuheishi/p/9985072.html
今日推荐