Android File Delete DeleteFiles(File dir, FilenameFilter filter)

Here you can find the source of DeleteFiles(File dir, FilenameFilter filter)

Description

Deletes the files in a specific directory that satisfy the filter.

License

Apache License

Parameter

Parameter Description
dir the directory containig the files to be deleted
filter the selection filter for the files to be deleted; May be null, then all files in the dir will be deleted

Declaration

public static void DeleteFiles(File dir, FilenameFilter filter) 

Method Source Code

/*//w ww.j a va 2 s.  c o m
    Copyright 1996-2008 Ariba, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

    $Id: //ariba/platform/util/core/ariba/util/core/FileUtil.java#24 $
 */

import ariba.util.log.Log;
import java.io.File;
import java.io.IOException;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.Random;
import java.util.List;

public class Main{
    /**
        Deletes the files in a specific directory that satisfy the filter.

        @param dir the directory containig the files to be deleted
        @param filter the selection filter for the files to be deleted;
            May be null, then all files in the dir will be deleted
        @aribaapi ariba
     */
    public static void DeleteFiles(File dir, FilenameFilter filter) {
        if (dir != null && dir.isDirectory()) {
            File[] files = ListFiles(dir, filter);
            if (!ArrayUtil.nullOrEmptyArray(files)) {
                for (int i = 0; i < files.length; ++i) {
                    if (files[i].isFile()) {
                        deleteFile(files[i]);
                    }
                }
            }
        }
    }
    /**
        Lists the files in a specific directory that satisfy the filter.

        @param dir the directory containig the listing files
        @param filter the selection filter for the files to be listed;
            May be null, then all files in the dir will be listed
        @return the file list
        @aribaapi ariba
     */
    public static File[] ListFiles(File dir, FilenameFilter filter) {
        File[] files = null;
        if (dir != null && dir.isDirectory()) {
            files = dir.listFiles(filter);
        }
        return files;
    }
    /**
        Convenience method that will call <code>delete()</code> on
        <code>file</code> and if that fails will call <code>file.deleteOnExit()</code>
        scheduling the file for deletion upon normal server exit. <p/>

        @param file the <code>File</code> to delete
        @return <code>true</code> if <code>file</code> was immediately deleted and
                <code>false</code> otherwise
        @aribaapi ariba
     */
    public static boolean deleteFile(File file) {
        boolean result = file.delete();
        if (!result) {
            Log.util.warning(8498, file);
            file.deleteOnExit();
        }
        return result;
    }
    /**
        Deletes the file identified by <code>toDelete</code>. <p/>

        @throws FileDeletionException if <code>toDelete</code> cannot be deleted
        @aribaapi ariba
     */
    public static void delete(File toDelete) throws FileDeletionException {
        if (!toDelete.delete()) {
            throw new FileDeletionException(toDelete);
        }
        // Note: sometimes the delete is successful but the
        // file still exists on the disk.  And it seems that calling
        // 'exists' can help complete the first 'delete' operation.
        if (toDelete.exists()) {
            Log.util.warning(8806, toDelete);
            throw new FileDeletionException(toDelete);
        }
    }
}

Related

  1. delFile(String path)
  2. delete(File f)
  3. delete(File file)
  4. delete(File toDelete)