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;
}
}
文章题目:java_io_面向对象风格递归获得文件夹大小
分享链接:
http://kswsj.cn/article/jhdeho.html