Java File Path Delete deleteDirectory(String path)

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

Description

Delete the directory by a given file path.

License

Open Source License

Parameter

Parameter Description
path the directory path to be deleted

Return

true if delete successfully.

Declaration

public static boolean deleteDirectory(String path) 

Method Source Code


//package com.java2s;
/*/*w w w. j a v  a 2s. c  o  m*/
 * RED: RNA Editing Detector Copyright (C) <2014> <Xing Li>
 *
 * RED is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * RED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

import java.io.File;

public class Main {
    /**
     * Delete the directory by a given file path.
     *
     * @param path the directory path to be deleted
     * @return true if delete successfully.
     */
    public static boolean deleteDirectory(String path) {
        if (!path.endsWith(File.separator)) {
            path = path + File.separator;
        }
        File dirFile = new File(path);
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        boolean flag = true;
        File[] files = dirFile.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    flag = deleteFile(file.getAbsolutePath());
                    if (!flag)
                        break;
                } else {
                    flag = deleteDirectory(file.getAbsolutePath());
                    if (!flag)
                        break;
                }
            }
        }

        return flag && dirFile.delete();
    }

    /**
     * Delete the file by a given file path.
     *
     * @param path the file to be deleted
     * @return true if delete successfully.
     */
    public static boolean deleteFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (file.isFile() && file.exists()) {
            if (file.delete()) {
                flag = true;
            }
        }
        return flag;
    }
}

Related

  1. deleteDirectory(String path)
  2. deleteDirectory(String path)
  3. deleteDirectory(String path)
  4. deleteDirectory(String path)
  5. deleteDirectory(String path)
  6. deleteDirectory(String sPath)
  7. deleteDirectoryAndContents(final File srcPath)
  8. deleteDirectoryAndContentsRecursive(final File srcPath, final boolean deleteSrcDir)
  9. deleteDirectoryContents(File path)