001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.util;
018:
019: import java.io.File;
020:
021: /*
022: * File System Directory Utilities. Some utilities that java.io doesn't give us.
023: *
024: * rmdir() - removes a directory and all subdirectories and files underneath.
025: *
026: * @author David S. Taylor <a href="mailto:taylor@apache.org">David Sean Taylor</a>
027: * @version $Id: DirectoryUtils.java 517124 2007-03-12 08:10:25Z ate $
028: *
029: */
030: public class DirectoryUtils {
031: public static void main(String[] args) {
032: DirectoryUtils.rmdir(new File(args[0]));
033: }
034:
035: /**
036: * Removes a directory and all subdirectories and files beneath it.
037: *
038: * @param directory The name of the root directory to be deleted.
039: * @return boolean If all went successful, returns true, otherwise false.
040: *
041: */
042: public static final boolean rmdir(File dir) {
043: if (dir.isDirectory()) {
044: String[] children = dir.list();
045: for (int i = 0; i < children.length; i++) {
046: boolean success = rmdir(new File(dir, children[i]));
047: if (!success) {
048: return false;
049: }
050: }
051: }
052:
053: // The directory is now empty so delete it OR it is a plain file
054: return dir.delete();
055: }
056:
057: /**
058: * Recursive deletion engine, traverses through all subdirectories,
059: * attempting to delete every file and directory it comes across.
060: * NOTE: this version doesn't do any security checks, nor does it
061: * check for file modes and attempt to change them.
062: *
063: * @param path The directory path to be traversed.
064: *
065: */
066: // private static void deleteTraversal(String path)
067: // {
068: // File file = new File(path);
069: // if (file.isFile())
070: // {
071: // try
072: // {
073: // file.delete();
074: // }
075: // catch (Exception e)
076: // {
077: // log.error("Failed to Delete file: " + path + " : " , e);
078: // file.deleteOnExit(); // try to get it later...
079: // }
080: // }
081: // else if (file.isDirectory())
082: // {
083: // if (!path.endsWith(File.separator))
084: // path += File.separator;
085: //
086: // String list[] = file.list();
087: //
088: // // Process all files recursivly
089: // for(int ix = 0; list != null && ix < list.length; ix++)
090: // deleteTraversal(path + list[ix]);
091: //
092: // // now try to delete the directory
093: // try
094: // {
095: // file.delete();
096: // }
097: // catch (Exception e)
098: // {
099: // log.error("Failed to Delete directory: " + path + " : " , e);
100: // file.deleteOnExit(); // try to get it later...
101: // }
102: //
103: // }
104: // }
105: }
|