-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearchFile2.java
More file actions
218 lines (196 loc) · 3.93 KB
/
SearchFile2.java
File metadata and controls
218 lines (196 loc) · 3.93 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* @parameter: args
* args[0]: 要扫描的目录
* args[1]: 结果保存文件夹
* @function: 读取目标文件夹里面的所有文件,子目录递归访问
*
*/
package Test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class SearchFile2
{
BufferedWriter bw = null;
static FileWriter fw = null;
BufferedReader brInput = null;
File dirFile = null;
File txtFile = null;
static int level;
public SearchFile2()
{
this.level = 0;
this.brInput = new BufferedReader(new InputStreamReader(System.in));
}
public void createDirFile(String filename)
{
this.dirFile = new File(filename);
}
public void createTxtFile(String filename)
{
txtFile = new File(filename);
if (! txtFile.exists())
{
try
{
txtFile.createNewFile();
fw = new FileWriter(txtFile, false);
bw = new BufferedWriter(fw);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public boolean isTrueFile()
{
if (dirFile == null || ! dirFile.exists())
{
return false;
}
return true;
}
public void readPathAndDest()
{
String src = null;
System.out.println("请输入目录路径,输入*quit退出!");
try
{
while ((src = brInput.readLine()) != null)
{
if (src.equals("*quit"))
{
System.out.println("程序退出...");
System.exit(0);
}
createDirFile(src);
if (isTrueFile())
{
break;
}
else
{
System.out.println("Error: " + src + "no such directory or file!");
System.out.println("Please input again!");
System.out.println("请输入目录路径,输入*quit退出!");
}
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String dst = null;
System.out.println("请输入输出文件路径,输入*quit退出!");
try
{
while ((dst = brInput.readLine()) != null)
{
if (dst.equals("*quit"))
{
System.out.println("程序退出...");
System.exit(0);
}
createTxtFile(dst);
break;
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getName()
{
return dirFile.getName();
}
public File getFile()
{
return dirFile;
}
public static void searchDir(File file)
{
String charSeq = file.getName() + "/" + "\r\n"; // \n \n\r不行
try
{
fw.append(charSeq);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
level++;
File[] listFiles = file.listFiles();
for (int i = 0; i < listFiles.length; i++)
{
if (listFiles[i].isFile())
{
String format = ""; // 不能使用 null, 否则输出null
for (int j = 0; j < level; j++)
{
format += '\t';
}
format += listFiles[i].getName() + "\r\n";
try
{
fw.append(format);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if (listFiles[i].isDirectory())
{
String format = "";
for (int j = 0; j < level; j++)
{
format += '\t';
}
format += '\n';
try
{
fw.append(format);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
searchDir(listFiles[i]);
}
}
}
public static void main(String[] args)
{
SearchFile2 sfile = new SearchFile2();
if (args != null && args.length > 1)
{
sfile.createDirFile(args[0]);
sfile.createTxtFile(args[1]);
if (! sfile.isTrueFile())
{
System.out.println("Error: " + args[0] + "no such directory or file!");
System.exit(0);
}
}
else
{
sfile.readPathAndDest();
}
System.out.println("开始扫描目录:" + sfile.getName());
searchDir(sfile.getFile());
try
{
fw.flush();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("扫描完成!");
}
}