import java.io.File; public class Test { public static void main(String args[]){ try { listFile("E:",0); } catch (Exception e) { e.printStackTrace(); } } public static void listFile(String path,int space) throws Exception { File file = new File(path); String spaceStr=""; for(int i=0;i<space;i++){ spaceStr+="="; } space++; if(file.isFile()){ System.out.println(spaceStr+file.getName()); }else{ System.out.println(spaceStr+"|"+file.getName()+"|"); File files[] = file.listFiles(); if(files!=null&&files.length>0){//判断不是空文件夹 for (int i = 0; i < files.length; i++) { listFile(files[i].getPath(),space); } } } } }