JAVA打印文件夹结构

输入一个指定的路径,然后将该路径下的所有文件和文件夹全部打印出来:

按照类似下面的格式输出:

.../build
.../.../.timestamp
.../.../before-obfuscation.jar
.../.../compiled
.../.../.../box.png
.../.../.../cafe.png
.../.../.../checkedbox.png
.../.../.../choicegroup_icon.png
.../.../.../com

 

 

import java.io.*;

public class ShowFiles {
 private static StringBuffer sb;
 
 public void showFiles(File file, int deep) {
  if(deep>0)
  {
   sb=new StringBuffer();
    for(int i = 0;i<deep;i++){
      sb.append(".../");
     }
     System.out.println(sb+file.getName());  
  }

  if(!file.isDirectory())
   return;
  File[] children = file.listFiles();
  if(children.length == 0||children==null)
   return;
  for(int i=0;i<children.length;i++) {
   showFiles(children[i], deep + 1);
  }
 }

 public static void main(String[] args) {
  ShowFiles show = new ShowFiles();
  File file = new File("D://PirateStudio");
  show.showFiles(file, 0);
 }
}

 

你可能感兴趣的:(java)