Java Utililty Methods Directory Clear

List of utility methods to do Directory Clear

Description

The list of methods to do Directory Clear are organized into topic(s).

Method

voidcleanDirectory(File dir)
clean Directory
File[] files = dir.listFiles();
if (files != null) {
    for (File f : files) {
        if (f.isFile()) {
            boolean deleted = f.delete();
            if (!deleted) {
                throw new IOException("file:" + f.getAbsolutePath() + " not deleted");
        } else {
            deleteDirectory(f);
voidcleanDirectory(File dir)
clean Directory
if (dir.exists() && dir.isDirectory()) {
    for (File f : dir.listFiles()) {
        deleteRecursively(f);
voidcleanDirectory(File dir)
Cleans the content of the specified directory recursively.
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
    for (File file : files) {
        delete(file);
booleancleanDirectory(File dir)
clean Directory
if (!dir.isDirectory())
    return false;
for (File file : dir.listFiles()) {
    if (!deleteRecursive(file))
        return false;
return true;
FilecleanDirectory(File dir)
clean Directory
if (dir.isFile()) {
    return null;
delete(dir);
dir.mkdirs();
return dir;
booleancleanDirectory(File dir, boolean deleteDirectory)
Cleans up the dir and deletes it.
if (!dir.exists()) {
    return true;
if (!dir.isDirectory()) {
    throw new IllegalArgumentException(dir.getAbsolutePath() + " is not a directory");
File[] files = dir.listFiles();
if (files == null) {
...
voidcleanDirectory(File directory)
clean Directory
if (!directory.exists()) {
    String message = directory + " does not exist";
    throw new IllegalArgumentException(message);
if (!directory.isDirectory()) {
    String message = directory + " is not a directory";
    throw new IllegalArgumentException(message);
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++) {
    File file = files[i];
    if (file.isDirectory()) {
        deleteDirectory(file);
    } else {
        file.delete();
voidcleanDirectory(File directory)
Deletes a directory (including any contents) and recreates it so that it is empty
deleteDirectory(directory);
if (!directory.mkdirs())
    throw new IOException("Directory could not be re-created during clean: " + directory.getAbsolutePath());
voidcleanDirectory(File directory)
clean Directory
File[] files = directory.listFiles();
if (files != null) {
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            cleanDirectory(files[i]);
        files[i].delete();
voidcleanDirectory(File directory)
clean Directory
if (!directory.exists()) {
    String message = directory + " does not exist";
    throw new IllegalArgumentException(message);
if (!directory.isDirectory()) {
    String message = directory + " is not a directory";
    throw new IllegalArgumentException(message);
File[] files = directory.listFiles();
if (files == null) { 
    throw new IOException("Failed to list contents of " + directory);
IOException exception = null;
for (File file : files) {
    try {
        delete(file);
    } catch (IOException ioe) {
        exception = ioe;
if (null != exception) {
    throw exception;