首页 > 编程语言 > 详细

java _io_面向对象风格递归获得文件夹大小

时间:2019-07-23 11:46:52      阅读:81      评论:0      收藏:0      [点我收藏+]
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_面向对象风格递归获得文件夹大小

原文:https://blog.51cto.com/14437184/2422510

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!