Java Delete Folder deleteFolder(String folder)

Here you can find the source of deleteFolder(String folder)

Description

Removes a directory and all sub-directories and files beneath it.

License

Open Source License

Parameter

Parameter Description
folder The name of the root directory to be deleted.

Return

boolean If all went successful, returns true, otherwise false.

Declaration

public static boolean deleteFolder(String folder) throws IOException 

Method Source Code


//package com.java2s;
/*//from  www .  j  ava  2  s  . co m
 * Copyright (c) 2007-2016 AREasy Runtime
 *
 * This library, AREasy Runtime and API for BMC Remedy AR System, is free software ("Licensed Software");
 * you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either version 2.1 of the License,
 * or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * including but not limited to, the implied warranty of MERCHANTABILITY, NONINFRINGEMENT,
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 */

import java.io.*;

public class Main {
    /**
     * Removes a directory and all sub-directories and files beneath it.
     *
     * @param folder The name of the root directory to be deleted.
     * @return boolean If all went successful, returns true, otherwise false.
     */
    public static boolean deleteFolder(String folder) throws IOException {
        return deleteFolder(new File(folder));
    }

    /**
     * Removes a directory and all sub-directories and files beneath it.
     *
     * @param folder The name of the root directory to be deleted.
     * @return boolean If all went successful, returns true, otherwise false.
     */
    public static boolean deleteFolder(File folder) throws IOException {
        if (!folder.isDirectory()) {
            boolean executed = folder.delete();

            if (!executed) {
                folder.deleteOnExit();
                return true;
            } else {
                return executed;
            }
        }

        deleteFile(folder.getPath());

        boolean executed = folder.delete();
        if (!executed) {
            folder.deleteOnExit();
            return true;
        } else {
            return executed;
        }
    }

    /**
     * Recursive deletion engine, traverses through all sub-directories,
     * attempting to delete every file and directory it comes across.
     * NOTE: this version doesn't do any security checks, nor does it
     * check for file modes and attempt to change them.
     *
     * @param path The directory path to be traversed.
     */
    public static void deleteFile(String path) throws IOException {
        if (path == null)
            throw new IOException("Input file path is null");
        deleteFile(new File(path));
    }

    /**
     * Recursive deletion engine, traverses through all sub-directories,
     * attempting to delete every file and directory it comes across.
     * NOTE: this version doesn't do any security checks, nor does it
     * check for file modes and attempt to change them.
     *
     * @param file The directory <code>File</code> entity to be traversed.
     */
    public static void deleteFile(File file) throws IOException {
        if (file == null)
            throw new IOException("Input file path is null");

        if (file.isFile()) {
            boolean executed = file.delete();
            if (!executed)
                file.deleteOnExit();
        } else {
            if (file.isDirectory()) {
                File list[] = file.listFiles();

                // Process all files recursively
                for (int ix = 0; list != null && ix < list.length; ix++)
                    deleteFile(list[ix]);

                // now try to delete the directory
                boolean executed = file.delete();
                if (!executed)
                    file.deleteOnExit();
            }
        }
    }
}

Related

  1. deleteFolder(File folder)
  2. deleteFolder(File folder)
  3. deleteFolder(final File folder)
  4. deleteFolder(final File folder)
  5. deleteFolder(final String folder)
  6. deleteFolder(String folder)
  7. deleteFolder(String name)
  8. deleteFolderContents(File folder)
  9. deleteFolderContents(File folder)