Java Directory Delete nio deleteDirectory(final Path dir, final int maxDepth)

Here you can find the source of deleteDirectory(final Path dir, final int maxDepth)

Description

Delete a directory recursively We could use commons-io library for this, if it were using the new java.nio.file API available since Java 7, not the case so far.

License

Open Source License

Parameter

Parameter Description
dir directory
maxDepth maximum number of levels of directories to delete. A value of 0 means that only the starting file is visited.

Exception

Parameter Description
IllegalArgumentException if the maxDepth parameter is negative
IOException file deletion error

Declaration

public static void deleteDirectory(final Path dir, final int maxDepth)
        throws IOException, IllegalArgumentException 

Method Source Code

//package com.java2s;
/**/*from w w w .j av  a  2 s .co  m*/
 * Copyright (C) 2012-2016 Thales Services SAS.
 *
 * This file is part of AuthZForce CE.
 *
 * AuthZForce CE 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.
 *
 * AuthZForce CE 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 AuthZForce CE.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;

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

import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

import java.util.Collections;

public class Main {
    private static FileVisitor<Path> DELETING_FILE_VISITOR = new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(final Path file,
                final BasicFileAttributes attrs) throws IOException {
            if (attrs.isRegularFile()) {
                Files.delete(file);
            }

            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(final Path dir,
                final IOException exc) throws IOException {

            if (exc == null) {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }

            throw exc;
        }
    };

    /**
     * Delete a directory recursively
     * 
     * We could use commons-io library for this, if it were using the new java.nio.file API available since Java 7, not the case so far.
     * 
     * @param dir
     *            directory
     * @param maxDepth
     *            maximum number of levels of directories to delete. A value of 0 means that only the starting file is visited.
     * @throws IllegalArgumentException
     *             if the maxDepth parameter is negative
     * @throws IOException
     *             file deletion error
     */
    public static void deleteDirectory(final Path dir, final int maxDepth)
            throws IOException, IllegalArgumentException {
        Files.walkFileTree(dir, Collections.<FileVisitOption> emptySet(),
                maxDepth, DELETING_FILE_VISITOR);
    }
}

Related

  1. deleteDirectory(File directory)
  2. deleteDirectory(File directory)
  3. deleteDirectory(File file)
  4. deleteDirectory(File path)
  5. deleteDirectory(final File directory)
  6. deleteDirectory(Path dir)
  7. deleteDirectory(Path directory)
  8. deleteDirectory(Path directory)
  9. deleteDirectory(Path directory)