Java File Path Delete deleteFolder(String path)

Here you can find the source of deleteFolder(String path)

Description

Removes the specified folder path

License

Open Source License

Parameter

Parameter Description
path the folder path

Return

true if the folder removed

Declaration

private static boolean deleteFolder(String path) 

Method Source Code


//package com.java2s;
/*/*  w w w  .  java 2s  .  c  o  m*/
 * Copyright (c) 2010-2012 Zhao.Xiang<z405656232x@163.com> Holding Limited.
 * All rights reserved.
 * 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

public class Main {
    /**
     * Removes the specified folder path
     * 
     * @param path
     *         the folder path
     * @return true if the folder removed
     */
    private static boolean deleteFolder(String path) {
        if (!path.endsWith(File.separator)) {
            path = path + File.separator;
        }

        File f = new File(path);
        if (f.exists() && f.isDirectory()) {
            File[] files = f.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isFile()) {
                    deleteFile(files[i].getAbsolutePath());
                } else {
                    deleteFolder(files[i].getAbsolutePath());
                }
            }
        }

        if (f.delete()) {
            return true;
        }

        return false;
    }

    /**
     * Returns true if the specified path is directory
     * 
     * @param path
     *          path of the folder
     * @return true if path is directory; false otherwise
     */
    public static boolean isDirectory(String path) {
        File file = new File(path);
        return file.isDirectory();
    }

    /**
     * Removes the specified file
     * 
     * @param path
     *         the file path
     * @return true if the file removed
     */
    private static boolean deleteFile(String path) {
        File f = new File(path);
        if (f.exists() && f.isFile()) {
            f.delete();
            return true;
        }
        return false;
    }
}

Related

  1. deleteFolder(String folderPath)
  2. deleteFolder(String folderPath)
  3. deleteFolder(String folderPath)
  4. deleteFolder(String path)
  5. deleteFolder(String path)
  6. deleteFolder(String path)
  7. deleteFolder(String path)
  8. deleteFolder(String path)
  9. deleteFolder(String path)