IO 文件夹的常用处理


1 public static void checkDirectory(String path)
2 {
3 File file=new File(path);
4 if(!file.exists())
5 {
6 System.out.println("目标文件夹不存在,准备创建");
7 if(file.mkdirs())
8 {
9 System.out.println("文件夹创建成功!");
10 }
11 else
12 {
13 System.out.println("文件夹创建失败!");
14 }
15 }
16 else
17 System.out.println("目标文件夹已经存在");
18 }
19
20
21
22 public static void delAllFolder(String folderPath)
23 {
24 delAllFile(folderPath);
25 File file=new File(folderPath);
26 file.delete();
27 }
28
29
30 //删除一个目录下的所有文件
31 public static boolean delAllFile(String filePath)
32 {
33
34
35 boolean flag=false;
36
37 //new File
38 File file=new File(filePath);
39
40 //文件不存在
41 if(!file.exists())
42 {
43 return flag;
44 }
45
46 //如果只是一个文件直接删除
47 if(file.isFile())
48 {
49 file.delete();
50 return flag=true;
51 }
52
53
54 //如果filePath ,没有以 "/" 结尾 自动加上
55 if(!filePath.endsWith(File.separator))
56 {
57 filePath=filePath+File.separator;
58 }
59
60 //文件夹下的文件列表
61 String tempList[]=file.list();
62
63 File temp=null;
64
65 //遍历文件列表
66 for(String f:tempList)
67 {
68 //接受文件
69 temp=new File(filePath+f);
70
71 //如果是文件
72 if(temp.isFile())
73 {
74 temp.delete();
75 }
76
77 //如果是文件 递归调用
78 else if(temp.isDirectory())
79 {
80 delAllFile(filePath+f);
81 delAllFolder(filePath+f);
82 flag=true;
83 }
84
85 }
86
87 return flag;
88
89 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值