ludc
2023-05-23 0bea703067ceea46c3f70b3a34d2cf0f55fe3281
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
 
 
import com.vci.ubcs.code.service.ICodeClassifyService;
import com.vci.ubcs.code.vo.pagemodel.CodeClassifyVO;
import org.springframework.beans.factory.annotation.Autowired;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
 
//列出File的一些常用操作
public class util {
 
 
 
 
    /**
     * 遍历指定目录下(包括其子目录)的所有文件,并删除以 lastUpdated 结尾的文件
     * @param dir 目录的位置 path
     * @throws IOException
     */
    public static void listDirectory(File dir) throws IOException {
        if (!dir.exists())
            throw new IllegalArgumentException("目录:" + dir + "不存在.");
        if (!dir.isDirectory()) {
            throw new IllegalArgumentException(dir + " 不是目录。");
        }
        File[] files = dir.listFiles();
        if (files != null && files.length > 0) {
            for (File file : files) {
                if (file.isDirectory())
                    //递归
                    listDirectory(file);
                else{ // 删除以 lastUpdated 结尾的文件
                    String fileName = file.getName();
                    boolean isLastupdated = fileName.toLowerCase().endsWith("lastupdated");
                    if (isLastupdated){
                        boolean is_delete = file.delete();
                        System.out.println("删除的文件名 => " + file.getName() + "  || 是否删除成功? ==> " + is_delete);
                    }
                }
            }
        }
    }
 
}