Fork me on GitHub

文件目录删除操作类

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
package com.cicoding.demo.dir;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 文件目录删除操作类
**/
public class DeleteDirectory {
/**
* 删除空目录
*
* @param dir
* 将要删除的目录路径
*/
private static void doDeleteEmptyDir(String dir) {
boolean success = (new File(dir)).delete();
if (success) {
System.out.println("Successfully deleted empty directory: " + dir);
} else {
System.out.println("Failed to delete empty directory: " + dir);
}
}

/**
* 递归删除目录下的所有文件及子目录下所有文件
*
* @param dir
* 将要删除的文件目录
* @return boolean Returns "true" if all deletions were successful. If a
* deletion fails, the method stops attempting to delete and returns
* "false".
*/
private static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
// 递归删除目录中的子目录下
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}

public static void traverseFolder2(String path) {

File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (files.length == 0) {
System.out.println("文件夹是空的!");
return;
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
System.out.println("文件夹:" + file2.getAbsolutePath());
traverseFolder2(file2.getAbsolutePath());
} else {
System.out.println("文件:" + file2.getAbsolutePath());
}
}
}
} else {
System.out.println("文件不存在!");
}
}

public static void getDir(String strPath) {
File f = new File(strPath);
if (f.isDirectory()) {
File[] fList = f.listFiles();
for (File f1 : fList) {
System.out.println(f1.getName());
}
}
}

//获取N天前的截止日期
public static Date getNextDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -8);
date = calendar.getTime();
return date;
}

//删除 截止日期之前的文件夹
public static void getNextDay22(String strPath,Date date) {
Calendar ca = Calendar.getInstance();
// Date curDate;
// Date b;
// 假设现在你已经实例化了a和b
// curDate.after(b)返回一个boolean,如果curDate的时间在b之后(不包括等于)返回true
// b.before(curDate)返回一个boolean,如果b的时间在curDate之前(不包括等于)返回true
// curDate.equals(b)返回一个boolean,如果a的时间和b相等返回true
//获取删除的截止日期
Date curDate = getNextDay(date);
File f = new File(strPath);
if (f.exists()) {
File[] files = f.listFiles();
if (files.length == 0) {
System.out.println("文件夹是空的!");
return;
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
try {
Date d = sdf.parse(file2.getName() + " " + "00:00:00");
if(d.before(curDate)){
System.out.println(strPath + "/" + file2.getName());
deleteDir(new File(strPath + "/" + file2.getName()));
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("文件:" + file2.getAbsolutePath());
}
}
}
} else {
System.out.println("文件不存在!");
}
}


/**
* 测试
*/
public static void main(String[] args) {
// doDeleteEmptyDir("new_dir1");
// String newDir2 = "F:/data/20170525";
// boolean success = deleteDir(new File(newDir2));
// if (success) {
// System.out.println("Successfully deleted populated directory: " + newDir2);
// } else {
// System.out.println("Failed to delete populated directory: " + newDir2);
// }
// // traverseFolder2("F:/data");
// getDir("F:/data");


Date d = new Date();
getNextDay22("F:/data", d);
// Date nextDay = getNextDay(d);
// SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
// String dateNowStr = sdf.format(nextDay);
// System.out.println("格式化后的日期:" + dateNowStr);

}
}
微信打赏

赞赏是不耍流氓的鼓励

评论系统未开启,无法评论!