import java.io.File;public class FileTree { public static void main(String[] args) { printFileTree("D:\\AAAAASSOCWEBDATA",0); } public static void printFileTree(String driver, int level) { File file = new File(driver); printByLevel(file.getName(),level); if (file.exists()) { if(file.isDirectory()){ File[] children = file.listFiles(); for (int i = 0; i < children.length; i++) { printFileTree(children[i].getPath(),level+1); } } } } public static void printByLevel(String name,int level) { for (int i = 0; i < level; i++) { System.out.print(" "); } System.out.println("|-〄"+name); }}
结果如下 |-〄AAAAASSOCWEBDATA |-〄bom |-〄320923198906262416 |-〄320923198906262416.jpg |-〄81696e7ae8034cafa3d1c96d9c10f04f.jpg |-〄f395ef2b4038442a9b97e1c54362be0c.jpg |-〄contact |-〄t.docx |-〄video