Java File Path Delete deleteDir(File dir, boolean deleteRoot)

Here you can find the source of deleteDir(File dir, boolean deleteRoot)

Description

delete Dir

License

Open Source License

Declaration

public static boolean deleteDir(File dir, boolean deleteRoot) 

Method Source Code

//package com.java2s;
/*/*from   ww w  .j a  v  a  2  s  .  co m*/
 * JStock - Free Stock Market Software
 * Copyright (C) 2012 Yan Cheng CHEOK <yccheok@yahoo.com>
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.io.File;

public class Main {
    public static boolean deleteDir(File dir, boolean deleteRoot) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]), true);
                if (!success) {
                    return false;
                }
            }
        }

        // The directory is now empty so delete it
        if (deleteRoot) {
            return dir.delete();
        }

        return true;
    }

    public static boolean deleteDir(String dir, boolean deleteRoot) {
        return deleteDir(new File(dir), deleteRoot);
    }

    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        // The directory is now empty so delete it
        return dir.delete();
    }
}

Related

  1. deleteDir(File dir)
  2. deleteDir(File dir)
  3. deleteDir(File dir)
  4. deleteDir(File dir)
  5. deleteDir(File dir)
  6. deleteDir(File dir, boolean deleteSelf)
  7. deleteDir(File dir, int depth, boolean deleteRootDirectory)
  8. deleteDir(File dir, String dirPath)
  9. deleteDir(File directory)