Java 實(shí)例 - 打印目錄結(jié)構(gòu)
以下實(shí)例演示了使用 File 類的 file.getName() 和 file.listFiles() 方法來打印目錄結(jié)構(gòu):
/* author by w3cschool.cc FileUtil.java */ import java.io.File; import java.io.IOException; public class FileUtil { public static void main(String[] a)throws IOException{ showDir(1, new File("d:\\Java")); } static void showDir(int indent, File file) throws IOException { for (int i = 0; i < indent; i++) System.out.print('-'); System.out.println(file.getName()); if (file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) showDir(indent + 4, files[i]); } } }
以上代碼運(yùn)行輸出結(jié)果為:
-Java -----codes ---------string.txt ---------array.txt -----w3cschoolcc
更多建議: