java_计算文件夹大小(目录容量)

package experiment11.exp1;
import experiment9.FileName;
import java.io.File;
import java.io.IOException;
/**
* @Author xuchaoxin
* @Date 12/18/2020 9:10 PM
* @Version 1.0
*/
public class CountDirSize {
static long countSize = 0;
/**
* 该函数以绝对路径名字符串path为参数进行处理,注意,path所指的是dir还是file不需要知道(由函数进行分析即可)
* @param path
* @return
*/
static void countDirSize(String path) {
File pathFile = new File(path);
String[] list = pathFile.list();
if (pathFile.isDirectory()) {
for (String items : list) {
String subItem=path+File.separator+items;
//递归调用.
countDirSize(subItem);
}
} else {
//this is a file.
countSize += pathFile.length();//返回文件字节数(无法直接正确作用于文件夹)
/**
* Returns:
* The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist. Some operating systems may return 0L for pathnames denoting system-dependent entities such as devices or pipes.
*/
}
//return countSize;
}
public static long getCountSize(String path) {
countDirSize(path);
return countSize;
}
/*test the function:(it's ok)*/
public static void main(String[] args) {
System.out.println(getCountSize(FileName.fileName11_1));
}
}
posted @   xuchaoxin1375  阅读(8)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2023-07-15 AA@有理系数多项式@整系数多项式@本原多项式@有理多项式可约问题
2023-07-15 java_Comparator(比较器对象重写(java.util.Comparator)例程(配合java.util.Collections类中的sort方法))
2022-07-15 EM@不等式的基本性质
2022-07-15 math_排序不等式的推导
点击右上角即可分享
微信分享提示