Android检查内部存储中是否存在文件

时间:2014-03-22 13:22:09

标签: android

我有一个字符串数组的文件名,我想循环遍历该数组并检查内部文件目录中是否存在任何文件。如果他们中的任何一个不存在我想删除那里的那些?有谁知道怎么做?

4 个答案:

答案 0 :(得分:1)

单向:

    String[] paths = ...;

    for(String path: paths){
        File file = new File(path);

        if(file.exists()) 
            file.delete();    
    }

另一个:

    File dir = new File("/pathToDir");
    File[] files = dir.listFiles();

    for(File file : files){
        //You Should not Exist!!!
        file.delete();
    }

答案 1 :(得分:1)

String[] fileNames = {"a.txt", "b.txt", "c.txt"};

for(int i = 0; i < fileNames.length; i++) {
    File file = getBaseContext().getFileStreamPath(fileNames[i]);

    if (file.exists()) {

        file.delete();
    }
}

答案 2 :(得分:1)

//试试这段代码

    // enter path of your dirctory
public void getDcimFolderImage(String path)
{
    File dir = new File(path);
    Log.e("path ", "is " + path);
    File file[] = dir.listFiles();

    try {
        if (file.length > 0)
        {
            for (int i = 0; i < file.length; i++) 
            {
                if (file[i].isFile()) 
                {

                    if (file.exists()
                    {
                       // enter your code whatever your want
                    }
                    else
                    {
                     // enter your code whatever your want
                    }

                }
                else 
                {
                        getDcimFolderImage(file[i].getAbsolutePath());
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }


}

答案 3 :(得分:1)

            File dir1 = getApplicationContext().getDir("your directory",
                Context.MODE_PRIVATE);

              // list the folder under the directory

        for (File fdir : dir1.listFiles()) {
            if (fdir.isDirectory()) {

                //list of file under the folder

                for (File wavfile : fdir
                        .listFiles()) {
                    String str = wavfile.getName().toString();

                    if (str.equals("your delete file"))) {

                        wavfile.delete();
                    }
                }
            }