JAVA删除文件和子目录文件根据后缀或者匹配名称

JAVA删除文件和子目录文件根据后缀或者匹配名称

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
package com.tengnat.mes.common.utils.file;
 
import java.io.File;
 
/**
 * Created by SkinApe on 2020/5/13.
 */
public class FileUtil {
 
    /**
     * 删除文件,和子目录文件
     * @param path  删除父路径
     * @param type  1匹配名称 2匹配类型
     * @param rule  删除文件规则
     */
    public static void deleteFile(String path,int type,String[] rule){
        File file=new File(path);
        if (file.isDirectory()) {
            File temp = null;
            File[] filelist = file.listFiles();
            for (int i = 0; i < filelist.length; i++) {
                temp = filelist[i];
                for (int j=0;j<rule.length;j++){
                    if (type==1){
                        if (temp.getName().indexOf(rule[j])!=-1&&!temp.getName().endsWith("pdf"))//蓝牙打印不删除pdf文件。。
                        {
                            temp.delete();// 删除文件
                        }
                    }else{
                        if (temp.getName().endsWith(rule[j]))
                        {
                            temp.delete();// 删除文件
                        }
                    }
                 }
 
 
            }
            String[] children = file.list();
            //递归删除目录中的子目录下
            for (int i=0; i<children.length; i++) {
                deleteFile(path+"\\"+children[i],type,rule);
            }
        }
    }
}

程序员之家

程序员之家
请先登录后发表评论
  • 最新评论
  • 总共0条评论