java遍历文件目录并以树状显示在控制台

public class FileDemo {

  public static void main(String[] args) {

  File file = new File("E:\\abc");
  System.out.println(file.getName());
  treeWalk(file, 1);
}

public static void treeWalk(File dir, int level) {
  String preStr = "";
  for (int i = 0 ; i < level; i ++) {
    preStr += " ";
  }
  File[] files = dir.listFiles();
  for(int i = 0; i < files.length; i++) {
    System.out.println(preStr + files[i].getName());
    if(files[i].isDirectory()) {
      treeWalk(files[i], level+1);
    }
  }
}

}

你可能感兴趣的:(java)