代碼以下:java
import java.io.File; public class FileList { public static void main(String[] args) { File f = new File("A"); System.out.println(f.getName()); //根目錄 FileList.tree(f, 1); } private static void tree(File f, int level) { String preStr = ""; for (int i = 0; i < level; i++) { preStr += " "; } File[] childs = f.listFiles(); for (int i = 0; i < childs.length; i++) { System.out.println(preStr + childs[i].getName()); if (childs[i].isDirectory()) { tree(childs[i], ++level); } } } }