Android File Delete forceDelete(final File file)

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

Description

Deletes a file.

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;
import static java.nio.file.Files.isSymbolicLink;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

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

public class Main {
    /**//from w w w  .  jav  a 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;
    }
}

Related

  1. delfile(String path, String name)
  2. findDeleteClasspathFile(String filename)
  3. forceDelete(File file)
  4. forceDelete(File file)
  5. forceDelete(String file)
  6. recursiveDelete(File fileOrDir)
  7. recursiveDelete(File path)
  8. removeableFile(File folder, String fileName)
  9. renameFile(String strSrc, String strDest, boolean deleteIfExist)