简书链接:某2015年写的仿windowtree命令遍历打印
文章字数:315,阅读全文大约需要1分钟
项目源码
https://gitee.com/51bwn/JavaProject

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
**
* 目录递归
*/
public void dirRecurrencePrint(String filePath) {
File f = new File(filePath);
if (f != null) {
File[] files = f.listFiles();
for (File file : files) {
if (file.isFile()) {
System.out.println("文件:" + file.getPath());
} else {
System.out.println("目录:" + file.getPath());
dirRecurrencePrint(file.getPath());

}

}
}

}
/**
* 高仿windows 的tree 包括了文件夹 和文件生成└─-结尾
* @param filePath
* @param tab
*/
public void dirRecurrencePrint1(String filePath, String tab) {

File f = new File(filePath);
File[] fileDirs = f.listFiles();//列出所有文件和目录
String temp="│";
if(fileDirs==null){
return;
}
for (File file : fileDirs) {

//发现这里写 文件过滤器多此一举而且竟然无效 为什么多此一举 因为 文件目录在fileDirs里面的最后面的
if (file.isFile()) {
if(file==fileDirs[fileDirs.length-1])
{ temp="";
System.out.println(tab + "└─-" + file.getName());

}
else
{
System.out.println(tab + "├─-" + file.getName());
}

} else {
File[] dirs = f.listFiles(new FilterDir());//先取出所有文件夹然后跟最后一个文件夹判断
if(file==dirs[dirs.length-1])//此方法 在只有文件夹的时候有效
{
// tab=tab.replace("│", "");
//System.out.println("---------------------------");
temp="";
System.out.println(tab + "└─-" + file.getName());
}
else
{
System.out.println(tab + "├─-" + file.getName());

}

dirRecurrencePrint1(file.getPath(), tab + temp+" ");
}


}
/*
* ├─Com│ └─Ac└ ─ca
*/

}
public void dirRecurrencePrint(String filePath, String tab) {
File f = new File(filePath);
String temp="│";
File[] files = f.listFiles(new FilterDir());
for (File file : files) {
if (file.isFile()) {
/* if(file==files[files.length-1])
{
System.out.println(tab + "└─-" + file.getName());
}
else
{
System.out.println(tab + "├─-" + file.getName());
}*/

} else {
//tab=tab.replaceAll("│", " ");
if(file==files[files.length-1])//此方法 在只有文件夹的时候有效
{
//tab=tab.replaceAll("│", " ");
temp="";
System.out.println(tab + "└─-" + file.getName());//是结尾就删除 分割线标记

//tab=tab.replaceAll("\\\\│", " ");

// System.out.println("tab:"+tab);
}
else
{
System.out.println(tab + "├─-" + file.getName());
}

dirRecurrencePrint(file.getPath(),tab + temp+" ");
}


}
/*
* ├─Com│ └─Ac└ ─ca
*/

}

public void dirRecurrencePrint(File file) {
File[] files = file.listFiles();
for (File file_temp : files) {
if (file_temp.isFile()) {
System.out.println("文件:" + file_temp.getPath());
} else {
// System.out.println(file_temp.listf;
System.out.println("目录:" + file_temp.getPath());
dirRecurrencePrint(file_temp);
}

}

}

/**
* 删除 目下 子目录所有文件
*
* @param filePath
*/
public void dirRecurrenceDelFile(String filePath) {
File f = new File(filePath);
File[] files = f.listFiles();
for (File file : files) {
if (file.isFile()) {
System.out.println("已删除文件:" + file.getPath());
file.delete();
} else {
System.out.println("进入目录:" + file.getPath());
dirRecurrenceDelFile(file.getPath());

}

}

}

/**
* 删除目录下所有文件 和文件夹 不包含此目录本身
*
* @param filePath
*/
public void dirRecurrenceDel(String filePath) {
File f = new File(filePath);
File[] files = f.listFiles();
for (File file : files) {
if (file.isFile()) {
if (file.delete()) {
System.out.println("已删除文件:" + file.getPath());
} else {
System.out.println("删除文件失败:" + file.getPath());
}
}

else {
System.out.println("进入目录:" + file.getPath());
dirRecurrenceDelFile(file.getPath());

}
// 循环到最底层之后 删除



}
//此方法防止到for循环里将不会删除根文件夹
if (f.delete()) {
System.out.println("已删除目录:" + f.getPath());
} else {
System.out.println("删除目录失败:" + f.getPath());
}

}
}

window的结构是这样的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
│  .gitignore
│ applib.iml
│ build.gradle
│ proguard-rules.pro

├─build
│ ├─generated
│ │ │ mockable-android-26.v3.jar
│ │ │
│ │ ├─assets
│ │ │ └─shaders
│ │ │ ├─debug
│ │ │ └─release
│ │ ├─res
│ │ │ ├─pngs
│ │ │ │ ├─androidTest
│ │ │ │ │ └─debug
│ │ │ │ ├─debug
│ │ │ │ └─release
│ │ │ ├─resValues
│ │ │ │ ├─androidTest
│ │ │ │ │ └─debug
│ │ │ │ ├─debug
│ │ │ │ └─release
│ │ │ └─rs
│ │ │ ├─androidTest
│ │ │ │ └─debug
│ │ │ ├─debug
│ │ │ └─release
│ │ └─source
│ │ ├─aidl
│ │ │ ├─androidTest
│ │ │ │ └─debug
│ │ │ ├─debug
│ │ │ └─release
│ │ ├─apt
│ │ │ ├─debug
│ │ │ │ ├─android
│ │ │ │ │ └─databinding
│ │ │ │ │ DataBindingComponent.java
│ │ │ │ │
│ │ │ │ ├─cn
│ │ │ │ │ └─qssq666
│ │ │ │ │ └─rapiddevelopframe
│ │ │ │ │ │ BR.java
│ │ │ │ │ │
│ │ │ │ │ └─databinding
│ │ │ │ │ LayoutDataValuesBinding.java
│ │ │ │ │ RecyclerviewRefreshBinding.java
│ │ │ │ │ ViewDataEmptyBinding.java
│ │ │ │ │
│ │ │ │ └─com
│ │ │ │ └─android
│ │ │ │ └─databinding
│ │ │ │ └─library
│ │ │ │ └─baseAdapters
│ │ │ │ BR.java
│ │ │ │
│ │ │ └─release
│ │ │ ├─android
│ │ │ │ └─databinding
│ │ │ │ DataBindingComponent.java
│ │ │ │
│ │ │ ├─cn
│ │ │ │ └─qssq666
│ │ │ │ └─rapiddevelopframe
│ │ │ │ │ BR.java
│ │ │ │ │
│ │ │ │ └─databinding
│ │ │ │ LayoutDataValuesBinding.java
│ │ │ │ RecyclerviewRefreshBinding.java
│ │ │ │ ViewDataEmptyBinding.java
│ │ │ │
│ │ │ └─com
│ │ │ └─android
│ │ │ └─databinding
│ │ │ └─library
│ │ │ └─baseAdapters
│ │ │ BR.java
│ │ │
│ │ ├─buildConfig
│ │ │ ├─androidTest
│ │ │ │ └─debug
│ │ │ │ └─cn
│ │ │ │ └─qssq666
│ │ │ │ └─rapiddevelopframe
│ │ │ │ └─test
│ │ │ │ BuildConfig.java
│ │ │ │
│ │ │ ├─debug
│ │ │ │ └─cn
│ │ │ │ └─qssq666
│ │ │ │ └─rapiddevelopframe
│ │ │ │ BuildConfig.java
│ │ │ │
│ │ │ └─release
│ │ │ └─cn
│ │ │ └─qssq666
│ │ │ └─rapiddevelopframe
│ │ │ BuildConfig.java
│ │ │
│ │ ├─dataBinding
│ │ │ ├─baseClasses
│ │ │ │ ├─debug
│ │ │ │ └─release
│ │ │ └─trigger
│ │ │ ├─debug
│ │ │ │ └─android
│ │ │ │ └─databinding
│ │ │ │ └─layouts
│ │ │ │ DataBindingInfo.java
│ │ │ │
│ │ │ └─release
│ │ │ └─android
│ │ │ └─databinding
│ │ │ └─layouts
│ │ │ DataBindingInfo.java
│ │ │
│ │ ├─r
│ │ │ ├─androidTest
│ │ │ │ └─debug
│ │ │ │ ├─android
│ │ │ │ │ ├─arch
│ │ │ │ │ │ └─lifecycle
│ │ │ │ │ │ R.java
│ │ │ │ │ │
│ │ │ │ │ └─support
│ │ │ │ │ ├─compat
│ │ │ │ │ │ R.java
│ │ │ │ │ │
│ │ │ │ │ ├─coreui
│ │ │ │ │ │ R.java
│ │ │ │ │ │
│ │ │ │ │ ├─coreutils
│ │ │ │ │ │ R.java
│ │ │ │ │ │
│ │ │ │ │ ├─design
│ │ │ │ │ │ R.java
│ │ │ │ │ │
│ │ │ │ │ ├─fragment
│ │ │ │ │ │ R.java