Java Delete Tree delTree(String dirName, String excludedDirName)

Here you can find the source of delTree(String dirName, String excludedDirName)

Description

Recursive utility function that delete entire directory.

License

Open Source License

Parameter

Parameter Description
dirName Directory to delete.
excludedDirName Directory name that will not deleted. Can be <code>null</code>.

Declaration

static public void delTree(String dirName, String excludedDirName) 

Method Source Code

//package com.java2s;
/*/*from w ww  .  j a va 2s .  co  m*/
 * Copyright (c) 2008-2016, GigaSpaces Technologies, Inc. All Rights Reserved.
 *
 * Licensed 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 {
    /**
     * Recursive utility function that delete entire directory.
     *
     * @param dirName         Directory to delete.
     * @param excludedDirName Directory name that will not deleted. Can be <code>null</code>.
     **/
    static public void delTree(String dirName, String excludedDirName) {
        File dir = new File(dirName);
        File[] files = dir.listFiles();

        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                if (files[i].isFile())
                    files[i].delete();
                else {
                    if (excludedDirName != null
                            && files[i].getPath().equalsIgnoreCase(dir + File.separator + excludedDirName))
                        continue;

                    delTree(files[i].getPath(), excludedDirName);
                }
            } /* for */
            // remove current dir
            dir.delete();
        }

    }
}

Related

  1. deltree(File root)
  2. deltree(final String directory)
  3. deltree(String dir)
  4. deltree(String dir)
  5. deltree(String directory)