Java File Path Delete delete(File path)

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

Description

Delete the whole directory

License

Open Source License

Parameter

Parameter Description
path a parameter

Declaration

public static boolean delete(File path) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007-2010 Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is 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
 *
 * Contributor:/*from ww  w .jav a 2s  .c om*/
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import java.io.File;

public class Main {
    /**
     * Delete the whole directory
     * @param path
     */
    public static boolean delete(File path) {
        boolean res = true, tmp = true;
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    tmp = delete(files[i]);
                } else {
                    tmp = deleteFile(files[i]);
                }
                res = res && tmp;
            }
        }
        tmp = deleteFile(path);
        res = res && tmp;
        return res;
    }

    /**
     * Delete single file
     * @param file
     */
    public static boolean deleteFile(File file) {
        boolean res = false;
        if (file.exists()) {
            if (file.delete()) {
                res = true;
            }
        }
        return res;
    }
}

Related

  1. delDir(String path)
  2. delDirAndFile(String tempath)
  3. deleleFiles(String regex, String path)
  4. delEmptyPath(String path)
  5. delete(File path)
  6. delete(File path)
  7. delete(File path)
  8. delete(File path)
  9. delete(File path, String... exclude)