完成一个程序,打印一个树状的目录结构。子目录与父目录之间有两个空格的缩进

来源:互联网 发布:手机照片涂鸦软件 编辑:程序博客网 时间:2024/06/15 04:32

import java.io.File;

/**
 *完成一个程序,打印一个树状的目录结构。子目录与父目录之间有两个空格的缩进
 */
public class TreeDir {

 public static void main(String[] args) {
  String pathname = "C:/Documents and Settings/user/桌面/Lecture notes/";
  File f = new File(pathname);
  System.out.println(f.getName());
  TreeDir.tree(f,0);
 }


 private static void tree(File f,int level) {
  File[] childs = f.listFiles();
  level++;
  int j = 0;
  String preStr = "";
  while(j < level){
   preStr += "--------";
   j++;
  }
  for (int i = 0; i < childs.length; i++) {
   System.out.println(preStr + childs[i].getName());
   if (childs[i].isDirectory()) {
    tree(childs[i],level);
   }
  }
 }
}

原创粉丝点击