首页 > 编程语言 > 详细

java 文件目录树

时间:2019-04-13 14:18:08      阅读:203      评论:0      收藏:0      [点我收藏+]

1. 目标格式,使用tree命令时,目录树格式如下。

技术分享图片

 public class TreeTest {

  public static void main(String[] args) {
      File root = new File("${path}/apache-tomcat-7.0.93/webapps/manager/");
      tree(root, -1, 0, "");
  }    

  public static void tree(File file, int index, int parentIndex, String parentFix) {
      String nowFix = "";
      if (parentIndex == 0) {
         nowFix = parentFix + "  ";
      } else if(parentIndex > 0) {
         nowFix = parentFix + "| ";
      }
      System.out.println(nowFix + (parentIndex >= 0 ? "|-" : "") + file.name());
      
      if (file.isDirectory()) {
        files[] files = file.listFiles();
        if (files == null) return;
        List<File> fileList = Arrays.stream(files).sorted((f1, f2) -> f2.getName().compareToIgnoreCase(f1.getName())).collect(Collections.toList());
        for (int i = fileList.size() - 1; i >= 0; --i) {
File f = fileList.get(i); tree(f, i, index, nowFix); } } } }

3. 代码输出:

manager
|-images
| |-add.gif
| |-asf-logo.svg
| |-code.gif
| |-design.gif
| |-docs.gif
| |-fix.gif
| |-tomcat.gif
| |-update.gif
| |-void.gif
|-index.jsp
|-META-INF
| |-context.xml
|-status.xsd
|-WEB-INF
| |-jsp
| | |-401.jsp
| | |-403.jsp
| | |-404.jsp
| | |-sessionDetail.jsp
| | |-sessionsList.jsp
| |-web.xml
|-xform.xsl

java 文件目录树

原文:https://www.cnblogs.com/Unchastity/p/10700946.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!