Android Directory Clean cleanDirectory(final File directory)

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

Description

Clean a directory without delete it

Parameter

Parameter Description
directory directory to clean

Exception

Parameter Description

Declaration

public static void cleanDirectory(final File directory)
        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  ww  w.java  2s.  c om
     * 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;
    }

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

Related

  1. cleanDirectory(File directory)
  2. cleanDirectory(File directory)
  3. cleanDirectory(String directory)
  4. clearDir(String dirPath)