打印目录,如树命令

时间:2015-10-30 13:28:13

标签: java algorithm printing tree directory

我必须打印目录树。 我有以下代码:

public static void main(String[] args) throws Exception
{

  File root = new File("C:\\Users\\User\\Desktop\\example");

  if (!root.isDirectory())
  {
    throw new IllegalArgumentException("Invalid directory: " + root.getName());
  }

  int level = 0;
  System.out.println(renderFolder(root, level, new StringBuilder(), false));
}

private static StringBuilder renderFolder(File folder, int level, StringBuilder sb, boolean isLast)
{
  indent(sb, level, isLast).append(folder.getName()).append("\n");

  File[] objects = folder.listFiles();

  for (int i = 0; i < objects.length; i++)
  {
    boolean last = ((i + 1) == objects.length);

    if (objects[i].isDirectory())
    {
      renderFolder(objects[i], level + 1, sb, last);
    }
  }
  return sb;
}


private static StringBuilder indent(StringBuilder sb, int level, boolean isLast)
{

  for (int i = 1; i < level; i++)
  {
     sb.append("\u2502   ");
  }

  if (level > 0)
  {
    sb.append(isLast
      ? "\u2514\u2500\u2500"
      : "\u251c\u2500\u2500");
  }

  return sb;
}

使用此输出:

example
├──a
└──b
│   └──b1
│   │   ├──b11
│   │   │   └──b111
│   │   └──b12
│   │   │   ├──b121
│   │   │   └──b122
│   │   │   │   └──b1221

我没有设法删除无用的深线。怎么做?
我的回答是:“如何保持线路连接(与cmd的树命令一样多)?”

3 个答案:

答案 0 :(得分:2)

import java.io.File;
public class FileAssert {

/**
 * Pretty print the directory tree and its file names.
 * 
 * @param folder
 *            must be a folder.
 * @return
 */
public static String printDirectoryTree(File folder) {
    if (!folder.isDirectory()) {
        throw new IllegalArgumentException("folder is not a Directory");
    }
    int indent = 0;
    StringBuilder sb = new StringBuilder();
    printDirectoryTree(folder, indent, sb);
    return sb.toString();
}

private static void printDirectoryTree(File folder, int indent,
        StringBuilder sb) {
    if (!folder.isDirectory()) {
        throw new IllegalArgumentException("folder is not a Directory");
    }
    sb.append(getIndentString(indent));
    sb.append("+--");
    sb.append(folder.getName());
    sb.append("/");
    sb.append("\n");
    for (File file : folder.listFiles()) {
        if (file.isDirectory()) {
            printDirectoryTree(file, indent + 1, sb);
        } else {
            printFile(file, indent + 1, sb);
        }
    }

}

private static void printFile(File file, int indent, StringBuilder sb) {
    sb.append(getIndentString(indent));
    sb.append("+--");
    sb.append(file.getName());
    sb.append("\n");
}

private static String getIndentString(int indent) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < indent; i++) {
        sb.append("|  ");
    }
    return sb.toString();
}
}

答案 1 :(得分:1)

尝试这个,我在你的方法中添加了另一个名为hierarchyTree的参数来指示是否在某个级别,我们应该打印|或空的空间。

它用作堆栈,可以根据递归级别轻松维护文件夹级别。

private static StringBuilder renderFolder(File folder, int level, StringBuilder sb, boolean isLast, List<Boolean> hierarchyTree) {
    indent(sb, level, isLast, hierarchyTree).append(folder.getName()).append("\n");

    File[] objects = folder.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File current, String name) {
            return new File(current, name).isDirectory();
        }
    });

    for (int i = 0; i < objects.length; i++) {
        boolean last = ((i + 1) == objects.length);

        // this means if the current folder will still need to print subfolders at this level, if yes, then we need to continue print |
        hierarchyTree.add(i != objects.length - 1);
        renderFolder(objects[i], level + 1, sb, last, hierarchyTree);

        // pop the last value as we return from a lower level to a higher level
        hierarchyTree.remove(hierarchyTree.size() - 1);
    }
    return sb;
}


private static StringBuilder indent(StringBuilder sb, int level, boolean isLast, List<Boolean> hierarchyTree) {
    String indentContent = "\u2502   ";
    for (int i = 0; i < hierarchyTree.size() - 1; ++i) {
        // determines if we need to print | at this level to show the tree structure
        // i.e. if this folder has a sibling foler that is going to be printed later
        if (hierarchyTree.get(i)) {
            sb.append(indentContent);
        } else {
            sb.append("    "); // otherwise print empty space
        }
    }

    if (level > 0) {
        sb.append(isLast
                ? "\u2514\u2500\u2500"
                : "\u251c\u2500\u2500");
    }

    return sb;
}

示例结果:

example
├──a
├──b
│   └──b1
│       ├──b11
│       │   └──b111
│       └──b12
│           ├──b121
│           └──b122
│               └──b1221
└──c

答案 2 :(得分:0)

您必须保留信息是否项目是所有父级别的文件夹中的最后一项。也就是说,你应该使isLast成为一个数组。

例如,当您尝试打印项b111时,该列表应为[true, true, false, true]。第一个条目告诉您是否要打印垂直线,最后一个条目告诉您打印角落还是T型交叉点。