Description
clear out all files recursively in a directory, including the directory itself
License
Apache License
Parameter
Parameter | Description |
---|
dirName | a parameter |
Exception
Parameter | Description |
---|
RuntimeException | when something goes wrong |
Declaration
public static void deleteRecursiveDirectory(String dirName)
Method Source Code
//package com.java2s;
/*******************************************************************************
* Copyright 2012 Internet2/*from w w w . j a v a 2s .c o m*/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
import java.io.File;
public class Main {
/**
* clear out all files recursively in a directory, including the directory
* itself
* @param dirName
*
* @throws RuntimeException
* when something goes wrong
*/
public static void deleteRecursiveDirectory(String dirName) {
//delete all files in the directory
File dir = new File(dirName);
//if it doesnt exist then we are done
if (!dir.exists()) {
return;
}
//see if its a directory
if (!dir.isDirectory()) {
throw new RuntimeException("The directory: " + dirName
+ " is not a directory");
}
//get the files into a vector
File[] allFiles = dir.listFiles();
//loop through the array
for (int i = 0; i < allFiles.length; i++) {
if (-1 < allFiles[i].getName().indexOf("..")) {
continue; //dont go to the parent directory
}
if (allFiles[i].isFile()) {
//delete the file
if (!allFiles[i].delete()) {
throw new RuntimeException("Could not delete file: "
+ allFiles[i].getPath());
}
} else {
//its a directory
deleteRecursiveDirectory(allFiles[i].getPath());
}
}
//delete the directory itself
if (!dir.delete()) {
throw new RuntimeException("Could not delete directory: "
+ dir.getPath());
}
}
/**
* <p>Find the index of the given object in the array.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
*
* @param array the array to search through for the object, may be <code>null</code>
* @param objectToFind the object to find, may be <code>null</code>
* @return the index of the object within the array,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int indexOf(Object[] array, Object objectToFind) {
return indexOf(array, objectToFind, 0);
}
/**
* <p>Find the index of the given object in the array starting at the given index.</p>
*
* <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
*
* <p>A negative startIndex is treated as zero. A startIndex larger than the array
* length will return <code>-1</code>.</p>
*
* @param array the array to search through for the object, may be <code>null</code>
* @param objectToFind the object to find, may be <code>null</code>
* @param startIndex the index to start searching at
* @return the index of the object within the array starting at the index,
* <code>-1</code> if not found or <code>null</code> array input
*/
public static int indexOf(Object[] array, Object objectToFind,
int startIndex) {
if (array == null) {
return -1;
}
if (startIndex < 0) {
startIndex = 0;
}
if (objectToFind == null) {
for (int i = startIndex; i < array.length; i++) {
if (array[i] == null) {
return i;
}
}
} else {
for (int i = startIndex; i < array.length; i++) {
if (objectToFind.equals(array[i])) {
return i;
}
}
}
return -1;
}
/**
* null safe string compare
* @param first
* @param second
* @return true if equal
*/
public static boolean equals(String first, String second) {
if (first == second) {
return true;
}
if (first == null || second == null) {
return false;
}
return first.equals(second);
}
/**
* <p>Compares two objects for equality, where either one or both
* objects may be <code>null</code>.</p>
*
* <pre>
* ObjectUtils.equals(null, null) = true
* ObjectUtils.equals(null, "") = false
* ObjectUtils.equals("", null) = false
* ObjectUtils.equals("", "") = true
* ObjectUtils.equals(Boolean.TRUE, null) = false
* ObjectUtils.equals(Boolean.TRUE, "true") = false
* ObjectUtils.equals(Boolean.TRUE, Boolean.TRUE) = true
* ObjectUtils.equals(Boolean.TRUE, Boolean.FALSE) = false
* </pre>
*
* @param object1 the first object, may be <code>null</code>
* @param object2 the second object, may be <code>null</code>
* @return <code>true</code> if the values of both objects are the same
*/
public static boolean equals(Object object1, Object object2) {
if (object1 == object2) {
return true;
}
if ((object1 == null) || (object2 == null)) {
return false;
}
return object1.equals(object2);
}
}
Related
- deleteRecursive(final File file)
- deleteRecursive(final File file)
- deleteRecursive(final File file, final boolean collect)
- deleteRecursiveDir(File directory)
- deleteRecursiveDirectories(String mainDir)
- deleteRecursiveFileDir(File dir)
- deleteRecursively(File dir)
- deleteRecursively(File directory)
- deleteRecursively(File dirEntry)