public class MyFile {
public static void main(String[] args) {
String str = "D:\\idea\\workspace-test\\study";
long dirSize = getDirSize(str);
System.out.println(dirSize);//135126
}
public static long getDirSize(String str){
File sumFile = new File(str);
long size = 0;
//判断是否是文件,是文件直接返回大小
if(!sumFile.isFile()){
File[] files = sumFile.listFiles();
for (File file : files) {
if(file.isFile()){
size = size + file.length();
}else {
size = size + getDirSize(file.getPath());
}
}
}else {
size = size +sumFile.length();
}
return size;
}
}
返回值: 135126
标签:sumFile,File,利用,String,str,file,统计,size From: https://blog.csdn.net/feng_ling_01/article/details/136913948