Java Delete File or Directory deleteFileOrDirectoryRecursive(String dir)

Here you can find the source of deleteFileOrDirectoryRecursive(String dir)

Description

Recurse through children deleting all as we go

License

Open Source License

Parameter

Parameter Description
dir a parameter

Declaration

public static void deleteFileOrDirectoryRecursive(String dir) 

Method Source Code

//package com.java2s;
/**/*  www .j a v  a 2s . c o m*/
 * nz.govt.natlib.ndha.common.Common - Software License
 *
 * Copyright 2007/2008 National Library of New Zealand.
 * 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 
 *
 * or the file "LICENSE.txt" included with the software.
 *
 * 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.
 *
 * FileUtils.java
 * $Rev$
 * PlayerM
 * 26/11/2007
 *
 */

import java.io.File;

public class Main {
    /**
     * Recurse through children deleting all as we go
     *
     * @param dir
     */
    public static void deleteFileOrDirectoryRecursive(String dir) {
        File theDir = new File(dir);
        if (theDir != null) {
            deleteFileOrDirectoryRecursive(theDir);
        }
    }

    /**
     * Recurse through children deleting all as we go
     *
     * @param dir
     */
    public static void deleteFileOrDirectoryRecursive(File dir) {
        if (dir.isDirectory()) {
            for (File file : dir.listFiles()) {
                if (file.isDirectory()) {
                    deleteFileOrDirectoryRecursive(file);
                } else {
                    file.delete();
                }
            }
            dir.delete();
        } else {
            dir.delete();
        }
    }
}

Related

  1. deleteFileOrDir(File instanceFileOrDir)
  2. deleteFileOrDirectory(File dir)
  3. deleteFileOrDirectory(File file)
  4. deleteFileOrDirectory(File fileOrDirectory)
  5. deleteFileOrDirectory(String fileName)
  6. deleteFileOrDirOnExit(File filehandle)
  7. deleteFileOrFolder(File fileOrFolder)