Java Directory Delete nio deleteDirectory(final File directory)

Here you can find the source of deleteDirectory(final File directory)

Description

Deletes a directory recursively.

License

Apache License

Parameter

Parameter Description
directory directory to delete

Exception

Parameter Description

Declaration

public static void deleteDirectory(final File directory)
        throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import static java.nio.file.Files.isSymbolicLink;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**//  w w  w.  j ava2 s .c o  m
     * Deletes a directory recursively.
     * @param directory directory to delete
     * @throws java.io.IOException in case deletion is unsuccessful
     */
    public static void deleteDirectory(final File directory)
            throws IOException {
        if (!directory.exists()) {
            return;
        }

        if (!isSymbolicLink(directory.toPath())) {
            cleanDirectory(directory);
        }

        if (!directory.delete()) {
            final String message = "Unable to delete directory "
                    + directory + ".";
            throw new IOException(message);
        }
    }

    /**
     * Clean a directory without delete it
     * @param directory directory to clean
     * @throws java.io.IOException in case cleaning is unsuccessful
     */
    public static void cleanDirectory(final File directory)
            throws IOException {
        if (!directory.exists()) {
            final String message = directory + " does not exist";
            throw new IllegalArgumentException(message);
        }

        if (!directory.isDirectory()) {
            final String message = directory + " is not a directory";
            throw new IllegalArgumentException(message);
        }

        final File[] files = directory.listFiles();
        if (files == null) { // null if security restricted
            throw new IOException("Failed to list content of " + directory);
        }

        IOException exception = null;
        for (final File file : files) {
            try {
                forceDelete(file);
            } catch (final IOException ioe) {
                exception = ioe;
            }
        }

        if (null != exception) {
            throw exception;
        }
    }

    public static List<File> listFiles(File dir) {
        List<File> list = new ArrayList<File>();
        if (dir.exists()) {
            for (File f : dir.listFiles()) {
                if (f.isFile()) {
                    list.add(f);
                }
            }
        }

        return list;
    }

    public static List<File> listFiles(File dir, final String suffix) {
        List<File> list = new ArrayList<File>();
        if (dir.exists()) {
            for (File f : dir.listFiles(new FilenameFilter() {

                @Override
                public boolean accept(File fdir, String name) {

                    if (name.endsWith(suffix)) {
                        return true;
                    }
                    return false;
                }
            })) {
                if (f.isFile()) {
                    list.add(f);
                }
            }
        }

        return list;
    }

    /**
     * Deletes a file. If file is a directory, delete it and all sub-directories.
     * @param file file or directory to delete, must not be {@code null}
     */
    public static void forceDelete(final File file) throws IOException {
        if (file == null) {
            return;
        }

        if (file.isDirectory()) {
            deleteDirectory(file);
        } else {
            final boolean filePresent = file.exists();
            if (!file.delete()) {
                if (!filePresent) {
                    throw new FileNotFoundException("File not exist "
                            + file);
                }

                final String message = "Unable to delete file " + file;
                throw new IOException(message);
            }
        }
    }
}

Related

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