Java File Path Delete deletePath(File path)

Here you can find the source of deletePath(File path)

Description

Delete all files and sub-folders of the specified path, and then the specified path itself.

License

Open Source License

Parameter

Parameter Description
path The path to delete (normally a folder).

Declaration

public static boolean deletePath(File path) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Firestar Software, 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:/*from   w  ww.j a va 2 s  . c  om*/
 *     Firestar Software, Inc. - initial API and implementation
 *
 * Author:
 *     Gabriel Oancea
 *
 *******************************************************************************/

import java.io.*;

public class Main {
    /**
     * Delete all files and sub-folders of the specified path, and then the specified path itself. All exceptions are
     * ignored.
     * 
     * @param path The path to delete (normally a folder).
     */
    public static boolean deletePath(File path) {
        if (path == null)
            throw new IllegalArgumentException(
                    "Null path in FileUtil.deletePath()");
        boolean ok = true;
        try {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length && ok; i++) {
                if (files[i].exists() && files[i].isDirectory())
                    ok = deletePath(files[i]);
            }
            for (int i = 0; i < files.length && ok; i++) {
                if (files[i].exists() && !files[i].isDirectory()) {
                    ok = files[i].delete();
                }
            }
            if (ok) {
                if (!path.delete()) {
                    ok = false;
                }
            }
        } catch (Exception ex) {
            return false;
        }
        return ok;
    }
}

Related

  1. deleteMultipleFiles(String filePath, int numberOfFiles)
  2. deleteNonEmptyDirectory(String dirPath)
  3. deleteOldStorageFile(String filepath)
  4. deleteParent(String filePath)
  5. deletePath(File dirPath)
  6. deletePath(final File path)
  7. deletePathRecursive(File path)
  8. deleteQuietly(Object path)
  9. deleteRecursive(File path)