2013-03-06 95 views

回答

4

假設您創建在內部存儲的目錄,你可以得到子對象的數量在目錄這樣

File dir = context.getDir("somePath", Context.MODE_PRIVATE); 
File[] children = dir.listFiles(); 
if(children.length > 0) 
{ 
    // the directory is not empty 
} 
else 
{ 
    // the directory is empty. 
} 

這僅僅是一個快速的示例代碼。更好的方法是使用自定義FileFilterdir.listFiles()排除自我和父代別名,因爲我不確定它們將始終從結果列表中排除。

2

下面的代碼將檢查是否有文件夾已經存在,如果不創建一個,並警告你,如果錯誤創建一個:如果路徑存在與否和path.exists()簡單地爲您創建一個

// check if appfolder is created and if not through an 
    // exception 
    File path = new File("yourDir"); 
    if (!path.exists()) { 
     if (!path.mkdirs()) { 
      try { 
       throw new IOException(
         "Application folder can not be created. Please check if your memory is writable and healthy and then try again."); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return false; 
     } 
    } else { 
     Log.i("app", "directory is: " + path.getPath()); 
    } 

exists()檢查。