Android Directory Delete deleteFiles(String directory)

Here you can find the source of deleteFiles(String directory)

Description

Deletes all files located in this directory.

Parameter

Parameter Description
directory a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static void deleteFiles(String directory) throws Exception 

Method Source Code

//package com.java2s;

import java.io.File;

public class Main {
    /**//from www  .  j a v a 2  s  . com
     * Deletes all files in the given directory with the exception of the 
     * given file name identified in dontDelete. The dontDelete string
     * should only contain a filename that is relative to the screen capture
     * directory, in other words this should not be an absolute path names.
     * @param directory
     * @param dontDelete Filename that is excluded from deletion
     * @throws Exception 
     */
    public static void deleteFiles(String directory, String dontDelete)
            throws Exception {
        File dir = new File(directory);
        String[] list = dir.list();
        if (list.length > 0) {
            for (int i = 0; i < list.length; i++) {
                File file = new File(directory, list[i]);
                if (!file.getPath().equalsIgnoreCase(dontDelete)) {
                    file.delete();
                }
            }
        }
    }

    /**
     * Deletes all files in this directory with the exception of the 
     * file names given in the dontDelete array. The dontDelete array
     * should only contain filename that are relative to the screen capture
     * directory, in other words there should not be absolute path names.
     * @param directory
     * @param dontDelete String array of filenames that are excluded from deletion
     * @throws Exception 
     */
    public static void deleteFiles(String directory, String[] dontDelete)
            throws Exception {
        File dir = new File(directory);
        String[] list = dir.list();

        if (dontDelete == null) {
            // If no skip files were given, invoke normal delete
            deleteFiles(directory);
        } else if (dontDelete.length == 1 && dontDelete[0] != null) {
            // If there is only one non-null dontDelete, send to proper function
            deleteFiles(directory, dontDelete[0]);
        } else if (list.length > 0) {
            // Delete all file that are not included in dontDelete array
            for (int i = 0; i < list.length; i++) {
                File file = new File(directory, list[i]);

                boolean deleteFile = true;
                // Check if current file is in the dontDelete
                for (String skipFile : dontDelete) {
                    if (file.getAbsolutePath().equals(skipFile)) {
                        deleteFile = false;
                        break;
                    }
                }

                if (deleteFile) {
                    file.delete();
                }
            }
        }
    }

    /**
     * Deletes all files located in this directory.
     * @param directory
     * @throws Exception 
     */
    public static void deleteFiles(String directory) throws Exception {
        File dir = new File(directory);
        String[] list = dir.list();
        if (list.length > 0) {
            for (int i = 0; i < list.length; i++) {
                File file = new File(directory, list[i]);
                file.delete();
            }
        }
    }
}

Related

  1. deleteDirectory(File path)
  2. deleteDirectory(String directory)
  3. deleteDirectory(String path)
  4. deleteDirectory(String path)
  5. deleteDirectory(final File directory)
  6. deleteFiles(String directory, String dontDelete)
  7. deleteFiles(String directory, String[] dontDelete)
  8. deleteSubdirs(String directory)
  9. removerDiretorio(File dir)