Java Path Delete nio delete(Path... paths)

Here you can find the source of delete(Path... paths)

Description

Deletes all given Paths recursively.

License

Open Source License

Parameter

Parameter Description
paths The Paths to delete.

Exception

Parameter Description
IOException if an error occurred during deletion

Declaration

public static void delete(Path... paths) throws IOException 

Method Source Code

//package com.java2s;
/**//from w ww.java 2  s  .c  om
 * This program 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.
 *
 * This program 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 this program.  If not, see http://www.gnu.org/licenses/.
 */

import java.io.IOException;

import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;

public class Main {
    /**
     * Deletes all given Paths recursively.
     *
     * @param paths The Paths to delete.
     * @throws IOException if an error occurred during deletion
     */
    public static void delete(Path... paths) throws IOException {
        for (Path path : paths) {
            if (!Files.exists(path)) {
                continue;
            }
            List<Path> childPaths = collect(path);
            Collections.reverse(childPaths);
            for (Path childPath : childPaths) {
                Files.delete(childPath);
            }
        }
    }

    /**
     * @param path The file or directory to collect paths from
     * @param pathMatchers The matchers to filter paths with
     * @return All matched paths within the given path or the path itself if it is a file and has been matched
     * @throws IOException if path is a directory and an error occurs visiting it
     */
    public static List<Path> collect(Path path, final PathMatcher... pathMatchers) throws IOException {
        final List<Path> paths = new LinkedList<>();
        if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    return collectPath(dir);
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    return collectPath(file);
                }

                private FileVisitResult collectPath(Path path) {
                    if (matches(path, pathMatchers)) {
                        paths.add(path);
                    }
                    return FileVisitResult.CONTINUE;
                }

            });
        }
        if (!paths.contains(path) && matches(path, pathMatchers)) {
            paths.add(0, path);
        }
        return paths;
    }

    private static boolean matches(Path path, PathMatcher... pathMatchers) {
        for (PathMatcher pathMatcher : pathMatchers) {
            if (!pathMatcher.matches(path)) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. delete(Path path)
  2. delete(Path path)
  3. delete(Path path)
  4. delete(Path root)
  5. delete(Path targetPath)
  6. delete(String filePath, String fileName)
  7. delete(String targetFilePath)
  8. deleteAllFilesRecursively(Path path)
  9. deleteContent(Path directory)