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     }


posted @ 2009-12-19 17:06  busing  阅读(126)  评论(0)    收藏  举报