1

I m a 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.

  • 1
    And what exactly is your question? Dont expect other people here to just read code you found somewhere; in order to figure what problems might be in that code in order to do all your work. Tell us what doesn't work; or where you are stuck. – GhostCat Jul 24 '15 at 10:22
  • You are not looking at extendions at all. Start doing that. Throw away String temp ....as you will use `file.delete()`there. But not on all files of course. – greenapps Jul 24 '15 at 10:25
  • Do you get any exceptions? Do you have permission to delete the file? – Andrew Fielden Jul 24 '15 at 10:26
  • I have permission under manifest file. –  Jul 24 '15 at 10:30
  • What's the output? Do you see "file .... is deleted : False"? – Andrew Fielden Jul 24 '15 at 10:32
  • Output is file....is deleted : True , but, the files are still there. –  Jul 24 '15 at 10:37
  • How to delete files with a certain extension from a folder in Android? this is my question @Jägermeister. It return True, but no files are deleted. –  Jul 24 '15 at 10:42
  • `I only want my encrypted file to remain `. Then tell what the used extension is. You do not need to define all those other extensions as you just have to check if the file has your extension. And if not delete it. – greenapps Jul 24 '15 at 10:52
  • `+ temp +`. Please tell what that prints. – greenapps Jul 24 '15 at 10:55
  • Encrypted file extension is .EXX, it print New Text Document.txt is deleted : true, but .zip and .mp4 are not deleted. Delete the txt only. –  Jul 24 '15 at 11:58

2 Answers2

2
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);
        }
    }
}
Amit Padekar
  • 259
  • 1
  • 6
  • 3
    Just throwing code at as answer is not a good practice. Try to explain what you are doing; and why. – GhostCat Jul 24 '15 at 11:27
  • While your answer is straightforward and doesn't justify the criticism about context, I believe you forgot to include the `GenericExtFilter`. I would assume it was borrowed from https://stackoverflow.com/a/10605576/461982 – Abandoned Cart Oct 20 '21 at 11:26
2

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();
        }
    }

}
Atul KS
  • 908
  • 11
  • 21