package com.test.tree;import java.io.File;/*** 列出一个目录中所有的文件和他们的大小* @author wyl**/ public class FileList {public void list(File f){ //文件根目录,深度为0list(f,0);}/*** * @param f* @param depth 为了显示文件的层级形式*/public void list(File f, int depth){printName(f, depth);if(f.isDirectory()){File[] files = f.listFiles();for(File file:files){list(file, depth+1);}}}/*** 循环打印文件名及文件大小* @param f* @param depth*/private void printName(File f, int depth) {// TODO Auto-generated method stubString name = f.getName();for(int i=0;i<depth;i++){ //缩进打印文件名System.out.print(" ");}if(f.isDirectory()){System.out.println("Dir: " + name);}else{System.out.println(f.getName() + "" + f.length());}}public static void main(String[] args) {FileList fileList = new FileList();File file = new File("C:/");fileList.list(file);} }