提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.workflow.core.util; |
懒 |
2 |
|
|
3 |
import java.io.File; |
|
4 |
|
|
5 |
/** |
|
6 |
* 删除文件工具类 |
|
7 |
* |
|
8 |
* @author fengshuonan |
|
9 |
* @Date 2019/8/7 23:14 |
|
10 |
*/ |
|
11 |
public class DelFileUtil { |
|
12 |
|
|
13 |
/** |
|
14 |
* 只删除此路径的最末路径下所有文件和文件夹 |
|
15 |
* |
|
16 |
* @param folderPath 文件路径 |
|
17 |
*/ |
|
18 |
public static void delFolder(String folderPath) { |
|
19 |
try { |
|
20 |
delAllFile(folderPath); // 删除完里面所有内容 |
|
21 |
String filePath = folderPath; |
|
22 |
filePath = filePath.toString(); |
|
23 |
java.io.File myFilePath = new java.io.File(filePath); |
|
24 |
myFilePath.delete(); // 删除空文件夹 |
|
25 |
} catch (Exception e) { |
|
26 |
e.printStackTrace(); |
|
27 |
} |
|
28 |
} |
|
29 |
|
|
30 |
/** |
|
31 |
* 删除指定文件夹下所有文件 |
|
32 |
* |
|
33 |
* @param path 文件夹完整绝对路径 |
|
34 |
*/ |
|
35 |
public static boolean delAllFile(String path) { |
|
36 |
boolean flag = false; |
|
37 |
File file = new File(path); |
|
38 |
if (!file.exists()) { |
|
39 |
return flag; |
|
40 |
} |
|
41 |
if (!file.isDirectory()) { |
|
42 |
return flag; |
|
43 |
} |
|
44 |
String[] tempList = file.list(); |
|
45 |
File temp = null; |
|
46 |
for (int i = 0; i < tempList.length; i++) { |
|
47 |
if (path.endsWith(File.separator)) { |
|
48 |
temp = new File(path + tempList[i]); |
|
49 |
} else { |
|
50 |
temp = new File(path + File.separator + tempList[i]); |
|
51 |
} |
|
52 |
if (temp.isFile()) { |
|
53 |
temp.delete(); |
|
54 |
} |
|
55 |
if (temp.isDirectory()) { |
|
56 |
delAllFile(path + "/" + tempList[i]); // 先删除文件夹里面的文件 |
|
57 |
delFolder(path + "/" + tempList[i]); // 再删除空文件夹 |
|
58 |
flag = true; |
|
59 |
} |
|
60 |
} |
|
61 |
return flag; |
|
62 |
} |
|
63 |
|
|
64 |
} |