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)); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享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_排序不等式的推导