递归方式遍历文件夹下所有子文件夹和文件
import java.io.File; import java.io.FileFilter; import org.junit.Test; public class TestFile { public static long size; public static void main(String[] args) { TestFile t = new TestFile(); String path = "C:/3000soft"; t.countSize(path); System.out.println("文件夹大小为:" + size / 1024.0 / 1024.0 + "M"); } public void countSize(String pathname) { File f = new File(pathname); if (!f.exists()) { return; } if (f.isFile()) { size += f.length(); System.out.println(f.getAbsolutePath()); return; } File[] child = f.listFiles(); for (int i = 0; i < child.length; i++) { countSize(child[i].getAbsolutePath()); } } }