简体   繁体   中英

How to delete files with a certain extension from a folder in Andorid

I ma newbie in Android. I generate a record audio file, generate a text file, zip the two files and encrypt them.

I want to delete the following extensions .txt, .mp4 and .zip. I only want my encrypted file to remain in my directory containing .txt and .mp4

I did research and come across the following source and try to modified it.

private static final String DEFAULT_STORAGE_DIRECTORY = "Recorder";
       private static final String FILE_RECORD_EXT = ".mp4";
       private static final String FILE_INI_EXT = ".txt";
       private static final String FILE_ZIP_EXT = ".zip";

       public static void main(String args[]) {
        new FileChecker().deleteFile(DEFAULT_STORAGE_DIRECTORY,FILE_RECORD_EXT,FILE_TXT_EXT);
       }

       public void deleteFile(String folder, String ext, String fileTxtExt){

         GenericExtFilter filter = new GenericExtFilter(ext);
         File dir = new File(folder);



         String[] list = dir.list(filter);

         if (list.length == 0) return;
         //Files 
         File fileDelete;

         for (String file : list){
         String temp = new StringBuffer(DEFAULT_STORAGE_DIRECTORY)
                          .append(File.separator)
                          .append(file).toString();
            fileDelete = new File(temp);
            boolean isdeleted = fileDelete.delete();
            System.out.println("file : " + temp + " is deleted : " + isdeleted);
         }
       }

       //inner class, generic extension filter 
       public class GenericExtFilter implements FilenameFilter {

           private String ext;

           public GenericExtFilter(String ext) {
             this.ext = ext;             
           }

           public boolean accept(File dir, String name) {
             return (name.endsWith(ext));
           }
        }
    }

Your help will be appreciated.

void deleteFiles(String folder, String ext)
{
    File dir = new File(folder);
    if (!dir.exists())
        return;
    File[] files = dir.listFiles(new GenericExtFilter(ext));
    for (File file : files)
    {
        if (!file.isDirectory())
        {
            boolean result = file.delete();
            Log.d("TAG", "Deleted:" + result);
        }
    }
}

Here is my working code for this. Please follow the comments inline to understand it's flow & function.

    //dirpath= Directory path which needs to be checked
    //ext= Extension of files to deleted like .csv, .txt
  public void deleteFiles(String dirPath, String ext) {
    File dir = new File(dirPath);
     //Checking the directory exists
    if (!dir.exists())
        return;
    //Getting the list of all the files in the specific  direcotry
    File fList[] = dir.listFiles();

    for (File f : fList) {
         //checking the extension of the file with endsWith method.
        if (f.getName().endsWith(ext)) {
            f.delete();
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM