All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.mada.jaxrs.utils.DirectoryDeleter Maven / Gradle / Ivy

There is a newer version: 0.11.8
Show newest version
package dk.mada.jaxrs.utils;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

/**
 * Deletes a directory.
 */
public final class DirectoryDeleter {
    private DirectoryDeleter() {
    }

    /**
     * Deletes directory.
     *
     * @param dir directory to delete
     * @throws UncheckedIOException if the operation fails
     */
    public static void delete(Path dir) {
        try {
            if (!Files.exists(dir)) {
                return;
            }

            Files.walkFileTree(dir, new FileVisitor() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    Files.delete(file);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            throw new UncheckedIOException("Failed to delete directory " + dir, e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy