public class test {
private int sum; //文件夹大小
private String path; //路径
private File f;
public static void main(String[]args) throws IOException
{
test t=new test("D:/d");
System.out.println(t.getSum());
}
public test(String path)
{
this.path=path;
this.f=new File(path);
N(this.f);
}
public int getSum()
{
return this.sum;
}
//递归打印获得文件夹大小
private int N(File f)
{
if(null!=f&&f.exists())
{
if(f.isFile()) //是文件才能获得大小
{
sum+=f.length();
}
else if(f.isDirectory())
{
File[] ss=f.listFiles();
for(File r:ss)
{
N(r);
}
}
}
return sum;
}
}
原文地址:https://blog.51cto.com/14437184/2422510
时间: 2024-09-29 00:27:17