Android Directory Delete deleteFiles(String directory, String[] dontDelete)

Here you can find the source of deleteFiles(String directory, String[] dontDelete)

Description

Deletes all files in this directory with the exception of the file names given in the dontDelete array.

Parameter

Parameter Description
directory a parameter
dontDelete String array of filenames that are excluded from deletion

Exception

Parameter Description
Exception an exception

Declaration

public static void deleteFiles(String directory, String[] dontDelete)
        throws Exception 

Method Source Code

//package com.java2s;

import java.io.File;

public class Main {
    /**/*w  ww .j a  v  a  2 s  .c o m*/
     * 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(String path)
  2. deleteDirectory(String path)
  3. deleteDirectory(final File directory)
  4. deleteFiles(String directory)
  5. deleteFiles(String directory, String dontDelete)
  6. deleteSubdirs(String directory)
  7. removerDiretorio(File dir)
  8. deleteDirectory(File path)
  9. deleteDir(File dir)