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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
package com.nis.nmsclient.util;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Contants;
/**
* 文件操作类:检索、排序
*
*/
public class FileUtil {
static Logger logger = Logger.getLogger(FileUtil.class);
/**
* 获取子目录列表
* @param file
* @return
*/
public static File[] getDirectoryArray(File file) {
if (!file.isDirectory()) {
new Exception(file.getAbsolutePath() + " Not A Directory");
}
return file.listFiles(new FilenameFilter() {
// 使用匿名内部类重写accept方法;
public boolean accept(File dir, String name) {
if (new File(dir, name).isDirectory()) {
return true;
} else
return false;
}
});
}
/**
* 获取指定全部或部分名称的文件
* @param file
* @param azz
* @return
*/
public static File[] getFilesArray(File file, final String azz) {
if (!file.isDirectory()) {
new Exception(file.getAbsolutePath() + " Not A Directory");
}
return file.listFiles(new FilenameFilter() {
// 使用匿名内部类重写accept方法;
public boolean accept(File dir, String name) {
if (new File(dir, name).isDirectory()) {
return false;
}
return name.indexOf(azz) != -1; // 过滤出指定全部名称或部分名称的文件
}
});
}
/**
* 获取指定前缀的文件
* @param file
* @param suffix
* @return
*/
public static File[] getFilesStartWith(File file, final String prefix) {
if (!file.isDirectory()) {
new Exception(file.getAbsolutePath() + " Not A Directory");
}
return file.listFiles(new FilenameFilter() {
// 使用匿名内部类重写accept方法;
public boolean accept(File dir, String name) {
if (new File(dir, name).isDirectory()) {
return false;
}
return name.startsWith(prefix); // 过滤出注定前缀名称的文件
}
});
}
/**
* 获取指定时间段指定前缀的文件
* @param file
* @param suffix
* @return
*/
public static File[] getFilesStartWithByMillis(File file, final String prefix, final long sTimeMillis, final long eTimeMillis) {
if (!file.isDirectory()) {
new Exception(file.getAbsolutePath() + " Not A Directory");
}
return file.listFiles(new FilenameFilter() {
// 使用匿名内部类重写accept方法;
public boolean accept(File dir, String name) {
if (new File(dir, name).isDirectory()) {
return false;
}
if (sTimeMillis > 0 && eTimeMillis > 0) {
if (new File(dir, name).lastModified() < sTimeMillis
|| new File(dir, name).lastModified() > eTimeMillis) {// 不在指定时间段
return false;
}
}
return name.startsWith(prefix); // 过滤出注定前缀名称的文件
}
});
}
/**
* 获取指定后缀名的文件
* @param file
* @param suffix
* @return
*/
public static File[] getFilesEndWith(File file, final String suffix) {
if (!file.isDirectory()) {
new Exception(file.getAbsolutePath() + " Not A Directory");
}
return file.listFiles(new FilenameFilter() {
// 使用匿名内部类重写accept方法;
public boolean accept(File dir, String name) {
if (new File(dir, name).isDirectory()) {
return false;
}
return name.endsWith(suffix); // 过滤出注定后缀名称的文件
}
});
}
/**
* 获取指定时间修改的指定后缀名的文件
* @param file
* @param suffix
* @return
*/
public static File[] getFilesEndWithBeforeMillis(File file, final String suffix, final long timeMillis) {
if (!file.isDirectory()) {
new Exception(file.getAbsolutePath() + " Not A Directory");
}
return file.listFiles(new FilenameFilter() {
// 使用匿名内部类重写accept方法;
public boolean accept(File dir, String name) {
if (new File(dir, name).isDirectory() || new File(dir,name).lastModified() > timeMillis) {
return false;
}
return name.endsWith(suffix); // 过滤出注定后缀名称的文件
}
});
}
/**
* 获取指定时间修改的文件
* @return
*/
public static File[] getFilesBeforeMillis(File file, final long timeMillis) {
if (!file.isDirectory()) {
new Exception(file.getAbsolutePath() + " Not A Directory");
}
return file.listFiles(new FilenameFilter() {
// 使用匿名内部类重写accept方法;
public boolean accept(File dir, String name) {
if (new File(dir, name).isDirectory() || new File(dir,name).lastModified() > timeMillis) {
return false;
}
return true;
}
});
}
/**
* 获取指定毫秒之后修改过的文件
* @param file
* @param timeMillis
* @return
*/
public static File[] getFilesAfterMillis(File file, final long timeMillis) {
if (!file.isDirectory()) {
new Exception(file.getAbsolutePath() + " Not A Directory");
}
return file.listFiles(new FilenameFilter() {
// 使用匿名内部类重写accept方法;
public boolean accept(File dir, String name) {
if (new File(dir, name).lastModified() >= timeMillis) {
return true;
} else
return false;
}
});
}
/**
* 获取指定时间名称及之前的文件夹
* @param file
* @param suffix
* @return
*/
public static File[] getDirsBeforeDateName(File file, final String yyyyMMddName) {
if (!file.isDirectory()) {
new Exception(file.getAbsolutePath() + " Not A Directory");
}
return file.listFiles(new FilenameFilter() {
// 使用匿名内部类重写accept方法;
public boolean accept(File dir, String name) {
if (new File(dir, name).isDirectory() && name.compareTo(yyyyMMddName)<=0) {
return true;
}
return false;
}
});
}
/**
* 获取指定时间名称及之后的文件夹
* @param file
* @param suffix
* @return
*/
public static File[] getDirsAfterDateName(File file, final String yyyyMMddName) {
if (!file.isDirectory()) {
new Exception(file.getAbsolutePath() + " Not A Directory");
}
return file.listFiles(new FilenameFilter() {
// 使用匿名内部类重写accept方法;
public boolean accept(File dir, String name) {
if (new File(dir, name).isDirectory() && name.compareTo(yyyyMMddName)>=0) {
return true;
}
return false;
}
});
}
/**
* 文件数组按最后修改日期排序,升序
* @param files
* @return
*/
public static File[] sortASCByModify(File[] files) {
Arrays.sort(files, new Comparator<File>(){
public int compare(File arg0, File arg1) {
if(arg0.lastModified() > arg1.lastModified()){
return 1;
}else if (arg0.lastModified() < arg1.lastModified()){
return -1;
}else {
return 0;
}
}
});
return files;
}
/**
* 文件数组按最后修改日期排序,降序
* @param files
* @return
*/
public static File[] sortDescByModify(File[] files) {
Arrays.sort(files, new Comparator<File>(){
public int compare(File arg0, File arg1) {
if(arg0.lastModified() > arg1.lastModified()){
return -1;
}else if (arg0.lastModified() < arg1.lastModified()){
return 1;
}else {
return 0;
}
}
});
return files;
}
/**
* 文件数组按名称排序,升序
* @param files
* @return
*/
public static File [] sortASCByFileName(File[] files) {
Arrays.sort(files, new Comparator<File>(){
public int compare(File arg0, File arg1) {
return arg0.getName().compareTo(arg1.getName());
}
});
return files;
}
/**
* 文件数组按名称排序,降序
* @param files
* @return
*/
public static File [] sortDescByFileName(File[] files) {
Arrays.sort(files, new Comparator<File>(){
public int compare(File arg0, File arg1) {
return arg1.getName().compareTo(arg0.getName());
}
});
return files;
}
/**
* 将文件移动到指定文件夹
*
* @param f
* 源文件
* @param newDir
* 新目录
* @param flag
* 是否覆盖 true:覆盖 false:不覆盖
*/
public static void moveFile(File f, String newDir, boolean flag) {
if (!f.exists()) {
logger.debug("文件已不存在,无法移动!" + f.getAbsolutePath());
return;
}
File fileDir = new File(newDir); // 备份目录
if (!fileDir.exists()) { // 检查并创建新目录
fileDir.mkdirs();
}
File dtnFile = new File(fileDir.getAbsolutePath() + File.separator
+ f.getName()); // 制定目标文件路径以及文件名
if (dtnFile.exists() && flag) { // 检查并删除已存在文件
//dtnFile.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(dtnFile);
logger.debug("moveFile 删除已存在文件--" + dtnFile.getAbsolutePath());
//FileUtil.checkParentDirExist(dtnFile);
}
try {
FileUtils.copyFile(f, dtnFile); // 移动文件
logger.debug("文件 From 》 " + f.getAbsolutePath()
+ "\n To 》 " + dtnFile.getAbsolutePath());
if (f.exists()) {
logger.debug("删除源文件" + f.getAbsolutePath());
//f.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(f);
//FileUtil.checkParentDirExist(f);
} else {
logger.debug("源文件不存在!不用删了!" + f.getAbsolutePath());
return;
}
} catch (IOException e) {
logger.error(Utils.printExceptionStack(e));
}
}
/**
* 删除文件或文件夹
*/
public static void delDir(File file) {
try {//文件在可删范围并且不在禁删范围
if (file.exists() && isIncludeDelRange(file) && !isExcludeDelRange(file)) {
if (!file.isDirectory()) {
file.delete();
logger.debug("FileUtil.delDir(File file)删除文件--" + file.getAbsolutePath());
FileUtil.checkParentDirExist(file);
} else if (file.isDirectory()) {
delDir2(file);
}
}
} catch (IOException e) {
logger.error(Utils.printExceptionStack(e));
}
}
/**
* 递归删除文件夹及其下的所有文件
*/
private static void delDir2(File file) throws IOException {
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
file.delete();
logger.debug("FileUtil.delDir2(File file)删除文件--" + file.getAbsolutePath());
FileUtil.checkParentDirExist(file);
} else if (file.isDirectory()) {
if (!isExcludeDelRange(file)) {// 文件不在禁删范围
for (File f : file.listFiles()) {
delDir2(f);
}
file.delete();
logger.debug("FileUtil.delDir2(File file)删除文件夹--" + file.getAbsolutePath());
FileUtil.checkParentDirExist(file);
}
}
}
/**
* 检查文件是否在可删范围内
* @param file
* @return true:是,false:否
*/
public static boolean isIncludeDelRange(File file) throws IOException{
boolean flag = false;
String path = file.getCanonicalPath();
if(Contants.COMMON_DEL_PATH_INCLUDE!=null){
for(String include : Contants.COMMON_DEL_PATH_INCLUDE){
if(path.startsWith(include)){
flag = true;
break;
}
}
}else{//删除范围参数为空,则返回在删除范围
flag = true;
}
return flag;
}
/**
* 检查文件是否在禁删范围内
* @param file
* @return true:是,false:否
*/
public static boolean isExcludeDelRange(File file) throws IOException{
boolean flag = false;
String path = file.getCanonicalPath();
if(Contants.COMMON_DEL_PATH_EXCLUDE!=null){
for(String exclude : Contants.COMMON_DEL_PATH_EXCLUDE){
if(path.startsWith(exclude)){
flag = true;
break;
}
}
}else{//禁删参数为空,则返回不在禁删范围
flag = false;
}
return flag;
}
public static void checkParentDirExist(File file){
logger.debug("检查父目录是否存在---" +file.getParentFile().exists() + "---" + file.getParent());
}
/**
* 格式化处理路径
* @param path
* @return
*/
public static String handlerPath(String path){
File file = new File(path);
try {
path = file.getCanonicalPath();
} catch (IOException e) {
logger.error(Utils.printExceptionStack(e));
}
return path;
}
public static File[] getAll(File dir, int level) {
logger.info(getLevel(level) + dir.getName());
level++;
File[] files = dir.listFiles();
for (int x = 0; x < files.length; x++) {
if (files[x].isDirectory())
getAll(files[x], level);
else
logger.debug(getLevel(level) + files[x].getName());
}
return files;
}
public static String getLevel(int level) {
StringBuilder sb = new StringBuilder();
sb.append("|--");
for (int x = 0; x < level; x++) {
sb.insert(0, "| ");
}
return sb.toString();
}
}
|