java 文件树目录_java 文件目录树

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

786d6c0d751417453fc95d6636a36fa1.png

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 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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值