Java Delete File Recursively deleteRecursively(File root)

Here you can find the source of deleteRecursively(File root)

Description

Delete the supplied File - for directories, recursively delete any nested directories or files as well.

License

Open Source License

Parameter

Parameter Description
root the root <code>File</code> to delete

Return

true if the File was deleted, otherwise false

Declaration

public static boolean deleteRecursively(File root) 

Method Source Code


//package com.java2s;
/*/*from ww  w  .ja  va 2  s .c  o  m*/
 * Copyright 1999-2004 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */

import java.io.File;

public class Main {
    /**
     * Delete the supplied {@link File} - for directories,
     * recursively delete any nested directories or files as well.
     * @param root the root <code>File</code> to delete
     * @return <code>true</code> if the <code>File</code> was deleted,
     * otherwise <code>false</code>
     */
    public static boolean deleteRecursively(File root) {
        if (root != null && root.exists()) {
            if (root.isDirectory()) {
                File[] children = root.listFiles();
                if (children != null) {
                    for (int i = 0; i < children.length; i++) {
                        deleteRecursively(children[i]);
                    }
                }
            }
            return root.delete();
        }
        return false;
    }
}

Related

  1. deleteRecursively(File fileOrDir)
  2. deleteRecursively(File fileOrDir)
  3. deleteRecursively(File fileToDelete)
  4. deleteRecursively(File fRoot)
  5. deleteRecursively(File name)
  6. deleteRecursively(File root)
  7. deleteRecursively(File root, boolean deleteRoot)
  8. deleteRecursively(File[] roots)
  9. deleteRecursively(final File directory)