Java File Path Delete deleteDirectory(final String path)

Here you can find the source of deleteDirectory(final String path)

Description

Deletes a file or directory

License

Open Source License

Parameter

Parameter Description
path - the full path

Exception

Parameter Description
IOException an exception

Declaration

public static void deleteDirectory(final String path) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 CA.  All rights reserved.
 *
 * This source file is licensed under the terms of the Eclipse Public License 1.0
 * For the full text of the EPL please see https://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

import java.io.File;

import java.io.IOException;

public class Main {
    /**//from ww  w .  j  a  va 2 s.co  m
     * Deletes a file or directory
     *   
     * @param path - the full path 
     * @throws IOException
     */
    public static void deleteDirectory(final String path) throws IOException {
        File file = new File(path);
        deleteDirectory(file);
    }

    /**
     * Deletes a directory. 
     * 
     * @param file - the file or directory
     * @throws IOException
     */
    public static void deleteDirectory(File file) throws IOException {

        if (file.isDirectory()) {

            //list all the directory contents
            String[] files = file.list();

            //directory is empty, then delete it
            if (files == null || files.length == 0) {
                deleteFileOrDirectory(file);
            } else {

                for (String temp : files) {
                    //construct the file structure
                    File fileDelete = new File(file, temp);

                    //recursive delete
                    deleteDirectory(fileDelete);
                }

                //check the directory again, if empty then delete it
                files = file.list();
                if (files == null || files.length == 0) {
                    deleteFileOrDirectory(file);
                }
            }

        } else {
            //if file, then delete it
            deleteFileOrDirectory(file);
        }

    }

    /**
     * Delete a directory or file 
     * 
     * @param file - the file or directory
     * @throws IOException
     */
    public static void deleteFileOrDirectory(File file) throws IOException {
        if (file.delete())
            System.out.println("Deleted : " + file.getAbsolutePath());
        else
            System.out.println("Failed to delete : " + file.getAbsolutePath());
    }
}

Related

  1. deleteDirectory(File path, boolean includeDir)
  2. deleteDirectory(final File path)
  3. deleteDirectory(final File path)
  4. deleteDirectory(final String directoryPath)
  5. deleteDirectory(final String fullDirPath)
  6. deleteDirectory(String dir_path)
  7. deleteDirectory(String directoryPath)
  8. DeleteDirectory(String directoryPath)
  9. deleteDirectory(String dirPath)