简体   繁体   English

如何从Andorid的文件夹中删除具有特定扩展名的文件

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

I ma newbie in Android.我是 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.我想删除以下扩展名 .txt、.mp4 和 .zip。 I only want my encrypted file to remain in my directory containing .txt and .mp4我只希望我的加密文件保留在包含 .txt 和 .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();
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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