Java Delete Directory deleteDirectory(File fileOrDir)

Here you can find the source of deleteDirectory(File fileOrDir)

Description

Delete a file system file or directory (and all the files and sub-directories it may contain).

License

Open Source License

Parameter

Parameter Description
fileOrDir The file or directory to be deleted.

Return

True or false, to indicate whether the deletion was successful.

Declaration

public static boolean deleteDirectory(File fileOrDir) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 Arapiki Solutions Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/* w ww .j a  v  a2 s.  c  om*/
 *    "Peter Smith <psmith@arapiki.com>" - initial API and 
 *        implementation and/or initial documentation
 *******************************************************************************/

import java.io.File;

public class Main {
    /**
     * Delete a file system file or directory (and all the files and sub-directories it may
     * contain).
     * @param fileOrDir The file or directory to be deleted.
     * @return True or false, to indicate whether the deletion was successful.
     */
    public static boolean deleteDirectory(File fileOrDir) {
        if (fileOrDir.isDirectory()) {
            String[] children = fileOrDir.list();
            for (int i = 0; i < children.length; i++) {
                if (!deleteDirectory(new File(fileOrDir, children[i]))) {
                    return false;
                }
            }
        }
        return fileOrDir.delete();
    }
}

Related

  1. deleteDirectory(File file)
  2. deleteDirectory(File file)
  3. deleteDirectory(File file)
  4. deleteDirectory(File file)
  5. deleteDirectory(File file)
  6. deleteDirectory(File root)
  7. deleteDirectory(File root)
  8. deleteDirectory(File root)
  9. deleteDirectory(File sourceDirectory, FilenameFilter filter, boolean recursive, boolean deleteItself)