View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.util;
18  
19  import java.io.File;
20  
21  
22  /*
23   * File System Directory Utilities. Some utilities that java.io doesn't give us.
24   *
25   *    rmdir() - removes a directory and all subdirectories and files underneath.
26   *
27   * @author David S. Taylor <a href="mailto:taylor@apache.org">David Sean Taylor</a>
28   * @version $Id: DirectoryUtils.java 517124 2007-03-12 08:10:25Z ate $
29   *
30   */
31  public class DirectoryUtils
32  {
33      public static void main(String[] args)
34      {
35          DirectoryUtils.rmdir(new File(args[0]));
36      }
37  
38      /***
39       *  Removes a directory and all subdirectories and files beneath it.
40       *
41       * @param directory The name of the root directory to be deleted.
42       * @return boolean If all went successful, returns true, otherwise false.
43       * 
44       */
45      public static final boolean rmdir(File dir)
46      {    
47  		if (dir.isDirectory())
48  		{
49  			String[] children = dir.list();
50  			for (int i = 0; i < children.length; i++)
51  			{
52  				boolean success = rmdir(new File(dir, children[i]));
53  				if (!success)
54  				{
55  					return false;
56  				}
57  			}
58  		}
59  
60  		// The directory is now empty so delete it OR it is a plain file
61  		return dir.delete();
62      }
63  
64      /***
65       *  Recursive deletion engine, traverses through all subdirectories, 
66       *  attempting to delete every file and directory it comes across.
67       *  NOTE: this version doesn't do any security checks, nor does it 
68       *  check for file modes and attempt to change them.
69       *
70       * @param path The directory path to be traversed.
71       * 
72       */            
73  //    private static void deleteTraversal(String path)
74  //    {
75  //        File file = new File(path);
76  //        if (file.isFile()) 
77  //        {
78  //            try 
79  //            {
80  //                file.delete();
81  //            }
82  //            catch (Exception e)
83  //            {
84  //                log.error("Failed to Delete file: " + path + " : " , e);
85  //                file.deleteOnExit(); // try to get it later...
86  //            }
87  //        } 
88  //        else if (file.isDirectory()) 
89  //        {
90  //            if (!path.endsWith(File.separator))
91  //                path += File.separator;
92  //
93  //            String list[] = file.list();
94  //
95  //            // Process all files recursivly
96  //            for(int ix = 0; list != null && ix < list.length; ix++)
97  //                deleteTraversal(path + list[ix]);
98  //
99  //            // now try to delete the directory
100 //            try 
101 //            {
102 //                file.delete();
103 //            }
104 //            catch (Exception e)
105 //            {
106 //                log.error("Failed to Delete directory: " + path + " : " , e);
107 //                file.deleteOnExit(); // try to get it later...
108 //            }
109 //            
110 //        }
111 //    }            
112 }
113 
114