Java Path Delete nio clearAndDeleteDirecotry(Path path)

Here you can find the source of clearAndDeleteDirecotry(Path path)

Description

clear And Delete Direcotry

License

Apache License

Declaration

public static void clearAndDeleteDirecotry(Path path) 

Method Source Code

//package com.java2s;
/*/*  w  ww  .  jav  a 2s .c  om*/
 * Copyright 2014 Sebastian Kruse, Thorben Lindhauer
 * 
 * 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;

import java.nio.file.Path;

public class Main {
    public static void clearAndDeleteDirecotry(Path path) {
        clearDirectory(path);
        path.toFile().delete();
    }

    public static void clearDirectory(Path path) {
        File file = path.toFile();
        if (!file.exists() || !file.isDirectory()) {
            System.err.println("FileUtils.clearDirectory: " + file + " is not a valid directory.");
        } else {
            deleteChildren(file);
        }
    }

    private static void deleteChildren(File file) {
        for (File child : file.listFiles()) {
            if (child.isDirectory()) {
                deleteChildren(child);
            }
            child.delete();
        }
    }
}

Related

  1. cleanUp(boolean isDeleteOriginalFiles, Path[] filesToZip)
  2. createDeleteOnExitFile(Path path)
  3. delete(@Nullable Path path)
  4. delete(final Path path)
  5. delete(final String[] paths)