Java File Delete nio forceDelete(final File file)

Here you can find the source of forceDelete(final File file)

Description

Deletes a file.

License

Apache License

Parameter

Parameter Description
file file or directory to delete, must not be null

Declaration

public static void forceDelete(final File file) 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 {
    /**/*from w w w  . j  a va 2 s .c o  m*/
     * 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);
            }
        }
    }

    /**
     * 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;
    }
}

Related

  1. deleteRecursively(File fileOrDir)
  2. deleteRecursively(File root)
  3. deleteTestFile(String extension)
  4. forceDelete(File file)
  5. forceDelete(File file)
  6. forceDeletion(File fileToDelete)
  7. recDeleteDirFile(File f)
  8. recursiveDelete(File parent)
  9. safeDeleteFile(final String fileName)