2
13693261870
2022-09-16 653761a31dfeb50dd3d007e892d69c90bf0cdafc
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
package com.landtool.lanbase.modules.api.utils;
 
 
import java.io.File;
 
/**
* @Description: 删除文件
* @author ykm
 * @param
* @return
* @date 2018/4/19 16:42
*/
public class DeleteFileUtils {
 
    public static boolean deletefile(String filePath) {
        File file = new File(filePath);
        if(file.exists() && file.isFile()) {
            if(file.delete()) {
                System.out.println("删除文件成功");
                return true;
            }
            else  {
                System.out.println("删除文件失败");
                return false;
            }
        }
        else  {
            System.out.println(filePath + "此路径不存在文件");
            return false;
        }
    }
 
    public static boolean deleteDiretory(String dirPath) {
        if(!dirPath.endsWith(File.separator)) {
            dirPath = dirPath + File.separator;
            File file = new File(dirPath);
            boolean flag = false;
            if(file.exists() && file.isDirectory()) {
                File[] files = file.listFiles();
                for (int i=0;i<files.length;i++) {
                    if(files[i].isFile()) {
                        flag = DeleteFileUtils.deletefile(files[i].getAbsolutePath());
                        if(!flag) break;
                    }
                    else if(files[i].isDirectory()) {
                        flag = DeleteFileUtils.deleteDiretory(files[i].getAbsolutePath());
                        if(!flag) break;
                    }
                }
            }
            if(!flag) {
                System.out.println("删除文件夹失败");
                return false;
            }
            if(file.delete()) {
                System.out.println("删除文件夹成功");
                return true;
            }
            else {
                System.out.println("删除文件及失败");
                return false;
            }
        }
        else {
            System.out.println("文件夹不存在");
            return false;
        }
    }
 
    public static boolean deletefileUtils(String filepath) {
        File file = new File(filepath);
        if(file.exists()) {
            if(file.isFile()) {
                return DeleteFileUtils.deletefile(filepath);
            }
            else if (file.isDirectory()) {
                return DeleteFileUtils.deleteDiretory(filepath);
            }
            else {
                System.out.println("无法判断类型");
                return false;
            }
        }
        else {
            System.out.println("文件不存在");
            return false;
        }
    }
}