Java Path Remove nio removeDirectory(Path directory)

Here you can find the source of removeDirectory(Path directory)

Description

Remove a directory and all of its contents.

License

Open Source License

Parameter

Parameter Description
directory The directory to be removed.

Exception

Parameter Description
IOException If there was an error deleting the file tree. In this case,some of the files may have already been deleted.

Declaration

public static void removeDirectory(Path directory) throws IOException 

Method Source Code


//package com.java2s;
/*//from w  w  w.  j  av  a  2 s. c o m
 * This file is part of the Raster Storage Archive (RSA).
 *
 * The RSA is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * The RSA is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * the RSA.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2013 CRCSI - Cooperative Research Centre for Spatial Information
 * http://www.crcsi.com.au/
 */

import java.io.File;
import java.io.IOException;

import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;

import java.nio.file.SimpleFileVisitor;

import java.nio.file.attribute.BasicFileAttributes;

public class Main {
    /**
     * Remove a directory and all of its contents.
     * 
     * @param directory
     *            The directory to be removed.
     * @throws IOException
     *             If there was an error deleting the file tree. In this case,
     *             some of the files may have already been deleted.
     */
    public static void removeDirectory(Path directory) throws IOException {
        final Path canonicalPath = directory.toAbsolutePath().normalize();
        if (canonicalPath.equals(canonicalPath.getRoot()))
            throw new IOException("Refusing to delete root directory.");

        Files.walkFileTree(canonicalPath, new SimpleFileVisitor<Path>() {
            /**
             * Delete files.
             */
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            /**
             * Delete directory after files have been removed (above).
             */
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
                if (e == null) {
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                } else {
                    // directory iteration failed
                    throw e;
                }
            }
        });
    }

    /**
     * Remove a directory and all of its contents.
     * 
     * @param directory
     *            The directory to be removed.
     * @throws IOException
     *             If there was an error deleting the file tree. In this case,
     *             some of the files may have already been deleted.
     */
    public static void removeDirectory(File directory) throws IOException {
        removeDirectory(directory.toPath());
    }

    /**
     * Remove a directory and all of its contents.
     * 
     * @param directory
     *            The directory to be removed.
     * @throws IOException
     *             If there was an error deleting the file tree. In this case,
     *             some of the files may have already been deleted.
     */
    public static void removeDirectory(String directory) throws IOException {
        removeDirectory(new File(directory));
    }
}

Related

  1. appendOrRemove(LinkedList paths, DirectoryStream ds)
  2. recursiveRemoveFolder(Path folderPath)
  3. removeDirectory(Path directory)
  4. removeDirectory(String pathToDir)
  5. removeDirectoryIfItIsEmpty(Path directoryToRemove)
  6. removeDriveLetter(Path path)
  7. removeFile(final String removePath)