Java文件目录树状结构:控制台打印某个文件夹下的文件目录树状结构
1 package com.zhen.file; 2 3 import java.io.File; 4 5 /* 6 * 控制台打印某个文件夹下的文件目录树状结构 7 * 递归算法 8 */ 9 10 public class FileTree { 11 12 public static void main(String[] args) { 13 File file =new File("D:/Github/JavaTest"); 14 PrintFile(file, 0); 15 } 16 17 public static void PrintFile(File file,int iLevel) 18 { 19 for(int i=0;i<iLevel;i++) 20 { 21 System.out.print("-"); 22 } 23 System.out.println(file.getName()); 24 25 if(file.isDirectory()) 26 { 27 File[] arrFiles = file.listFiles(); //子目录 28 for(File fileItem : arrFiles) 29 { 30 PrintFile(fileItem, iLevel+1); 31 } 32 } 33 } 34 }
I don't extravagant hope to change the world, only hope this world will not change me.