特別な目的のため、SDCARD 内のディレクトリ内の特定の名前のディレクトリを除くすべてのディレクトリを一覧表示したいと考えています。
親の 1 つのmomに dir1、dir2、dir3、dir4の 4 つのディレクトリがあるとします。ここで、 dir1 、 dir2、およびdir4のみをリストし、 dir3 をリストしたくないので、リストには次のものがあります。
- dir1
- dir2
- dir4
プログラムでこれを行うにはどうすればよいですか? 以下の私のコードを見てください。どうもありがとうございました。
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");
}
}
}