0

由于特殊目的,我想列出 SDCARD 目录中的所有目录,但具有特定名称的目录除外。

假设我在父母 one mom中有四个目录dir1, dir2, dir3, dir4。现在我只想列出dir1、dir2dir4,而不是dir3,所以在列表中,有:

  • 目录1
  • 目录2
  • 目录4

我怎样才能以编程方式做到这一点?请在下面查看我的代码。非常感谢你。

public class DatabaseFileList {
static final private String FILELIST_TAG = "[DatabaseFileList]";

public ArrayList<DatabaseFile> items;

public DatabaseFileList(String dbPath, String dbExtension)
{       
    items = new ArrayList<DatabaseFile>();

    getDatabaseFileList(dbPath,dbExtension);
}

private void getDatabaseFileList(String dbPath, String dbExtension)   
{
    items.clear();

    File dataDirectory = new File(dbPath);
    if (!dataDirectory.exists()) //Data directory doesn't exist, create it
    {
        if (!dataDirectory.mkdirs())
        {
            Log.i(FILELIST_TAG, "Cannot create directory on sdcard"); 
        }
        else
        {
            Log.i(FILELIST_TAG, "Data directory was created on sdcard");
        }
    }

    File dataDirectoryAV = new File(dbPath + "dir1");
    if (!dataDirectoryAV.exists()) //Data directory doesn't exist, create it
    {
        if (!dataDirectoryAV.mkdirs())
        {
            Log.i(FILELIST_TAG, "Cannot create dir1 on sdcard"); 
        }
        else
        {
            Log.i(FILELIST_TAG, "dir1 was created on sdcard");
        }
    }

    File dataDirectoryVA = new File(dbPath + "dir2");
    if (!dataDirectoryVA.exists()) //Data directory doesn't exist, create it
    {
        if (!dataDirectoryVA.mkdirs())
        {
            Log.i(FILELIST_TAG, "Cannot create dir2 on sdcard"); 
        }
        else
        {
            Log.i(FILELIST_TAG, "dir2 was created on sdcard");
        }
    }

    File dataDirectoryVAkd = new File(dbPath + "dir3");
    if (!dataDirectoryVAkd.exists()) //Data directory doesn't exist, create it
    {
        if (!dataDirectoryVAkd.mkdirs())
        {
            Log.i(FILELIST_TAG, "Cannot create dir3 on sdcard"); 
        }
        else
        {
            Log.i(FILELIST_TAG, "dir3 was created on sdcard");
        }
    }

    File dataDirectoryVAkd = new File(dbPath + "dir4");
    if (!dataDirectoryVAkd.exists()) //Data directory doesn't exist, create it
    {
        if (!dataDirectoryVAkd.mkdirs())
        {
            Log.i(FILELIST_TAG, "Cannot create dir4 on sdcard"); 
        }
        else
        {
            Log.i(FILELIST_TAG, "dir4 was created on sdcard");
        }
    }

    FileFilter ffDir = new FileFilter()
    {
        public boolean accept(File f)
        {
            return f.isDirectory();
        }
    };

    File[] lstDirectory = dataDirectory.listFiles(ffDir);

    if (lstDirectory != null && lstDirectory.length > 0)
    {
        for (File currentDirectory : lstDirectory)
        {
            DatabaseFile db = new DatabaseFile();
            String path = currentDirectory.getAbsolutePath() + "/" + currentDirectory.getName();
            //Log.i(FILELIST_TAG,"Filelist path = " + path);

            db.fileName = currentDirectory.getName();
            db.path = currentDirectory.getPath();

            File ifoFile = new File(path + ".ifo");

            if (ifoFile.exists())   
            {

                String data;
                String arrData[] = null; 
                try
                {
                    BufferedReader brIfoFile = new BufferedReader(new FileReader(ifoFile));
                    while (brIfoFile.ready())
                    {
                        data = brIfoFile.readLine(); 
                        arrData = data.split("=");
                        arrData[0] = arrData[0].trim();
                        if (arrData[0].equals("name"))
                        {
                            db.dictionaryName = arrData[1].trim();

                        }
                        else if (arrData[0].equals("from"))
                        {
                            db.sourceLanguage = arrData[1].trim();
                            //Log.i(FILELIST_TAG, "from = " + arrData[1]);
                        }
                        else if (arrData[0].equals("to"))
                        {
                            db.destinationLanguage= arrData[1].trim();
                            //Log.i(FILELIST_TAG, "to = " + arrData[1]);
                        }
                        else if (arrData[0].equals("style"))
                        {
                            db.style= arrData[1].trim();
                            //Log.i(FILELIST_TAG, "style = " + arrData[1]); 
                        }
                    }
                }
                catch (Exception ex){
                    db.dictionaryName = db.fileName + " File not read!";
                    Log.e(FILELIST_TAG, "Can not read ifo file!");
                }
            }
            else
            {
                db.dictionaryName = db.fileName + " - No DB in sdcard/mom/";
                Log.i(FILELIST_TAG, "No ifo file found, set db name to file name");
            }
            //add to list
            items.add(db);
        }
        Log.i(FILELIST_TAG,"Found " + items.size() + " dictionaries");
    }
    else
    {
        Log.i(FILELIST_TAG,"Do not find any valid db");
    }
}
}
4

1 回答 1

2

实现 java.io.FilenameFilter ?或者您对结果运行过滤器(排除列表)。

于 2012-04-19T15:07:51.503 回答