How to delete directory in Java

  • 19 October 2016
  • ADM

 

How to delete directory in Java - images/logos/java.jpg

 

File class from io java.io package provides two methods to delete a folder.

  • delete() - Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.
  • deleteOnExit() - Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates. Files (or directories) are deleted in the reverse order that they are registered.

Example

Because delete method is deleting only files or empty folder you need to delete recursively all files and all sub-folders within the folder first.

For this will create an recursive delete method.

package com.admfactory.io;

import java.io.File;

public class DeleteFolderExample {

    /***
     *
     * Delete the file/directory received as parameter and all sub-directories.
     *
     * @param file
     * @return true or false
     */
    public boolean deleteAll(File file) {
	/**
	 * checking if the file parameter is a file - if yes just delete the
	 * file
	 */
	if (file.isFile()) {
	    boolean b = file.delete();
	    if (b) {
		System.out.println("File '" + file.getAbsolutePath() + "' - success");
	    } else {
		System.out.println("File '" + file.getAbsolutePath() + "' - failed");
	    }
	    return b;
	} else {
	    /** if is a directory */
	    /** check if the directory is empty */
	    String files[] = file.list();
	    /** if there is no file in the array we can delete the directory */
	    if (files.length == 0) {
		boolean b = file.delete();
		if (b) {
		    System.out.println("Directory '" + file.getAbsolutePath() + "' - success");
		} else {
		    System.out.println("Directory '" + file.getAbsolutePath() + "' - failed");
		}
		return b;
	    } else {
		/**
		 * if there are files in the array delete first all the files or
		 * folder then delete the folder
		 */
		for (String path : files) {
		    // construct the file structure
		    File fileDelete = new File(file, path);

		    /** call deleteAll for a recursive delete */
		    /**
		     * note that there is no return here as we want to delete
		     * all files from current directory
		     */
		    /**
		     * a return here would cause to delete only first file or
		     * directory within current directory
		     */
		    deleteAll(fileDelete);
		}

		/**
		 * check again if all the sub-directories were deleted, then
		 * delete the current folder
		 */
		files = file.list();
		if (files.length == 0) {
		    boolean b = file.delete();
		    if (b) {
			System.out.println("Directory '" + file.getAbsolutePath() + "' - success");
		    } else {
			System.out.println("Directory '" + file.getAbsolutePath() + "' - failed");
		    }
		    return b;
		}
		/**
		 * if is getting to this point it means that something went
		 * wrong on files or sub directories
		 */
		return false;
	    }
	}
    }

    public static void main(String[] args) {
	File dir = new File("c:\\admfactory");
	DeleteFolderExample example = new DeleteFolderExample();
	System.out.println("Delete '" + dir.getAbsolutePath() + "' folder.");
	System.out.println();
	boolean b = example.deleteAll(dir);
	System.out.println();
	if (b) {
	    System.out.println("Operation succedded.");
	} else {
	    System.out.println("Operation failed.");
	}
    }
}

Output

Delete 'c:\admfactory' folder.

File 'c:\admfactory\test1\adt_null.dll' - success
File 'c:\admfactory\test1\authentication_windows_client.dll' - success
File 'c:\admfactory\test1\ha_blackhole.dll' - success
File 'c:\admfactory\test1\ha_example.dll' - success
File 'c:\admfactory\test1\ha_test_sql_discovery.dll' - success
File 'c:\admfactory\test1\libdaemon_example.dll' - success
File 'c:\admfactory\test1\server_audit.dll' - success
File 'c:\admfactory\test1\sql_errlog.dll' - success
Directory 'c:\admfactory\test1' - success
Directory 'c:\admfactory\test2\a1' - success
File 'c:\admfactory\test2\a2\auth_test_plugin.dll' - success
File 'c:\admfactory\test2\a2\c1\sql_errlog.dll' - success
Directory 'c:\admfactory\test2\a2\c1' - success
File 'c:\admfactory\test2\a2\c2\file_key_management.dll' - success
File 'c:\admfactory\test2\a2\c2\simple_password_check.dll' - success
Directory 'c:\admfactory\test2\a2\c2' - success
File 'c:\admfactory\test2\a2\debug_key_management.dll' - success
File 'c:\admfactory\test2\a2\dialog_examples.dll' - success
File 'c:\admfactory\test2\a2\mysql_clear_password.dll' - success
File 'c:\admfactory\test2\a2\qa_auth_client.dll' - success
File 'c:\admfactory\test2\a2\qa_auth_server.dll' - success
Directory 'c:\admfactory\test2\a2' - success
Directory 'c:\admfactory\test2\a3' - success
Directory 'c:\admfactory\test2' - success
Directory 'c:\admfactory\test3' - success
Directory 'c:\admfactory' - success

Operation succedded.

As we expected first it was deleted all files and directories within the directory and then the directory itself.

 

References