2012-02-26 83 views
0

這是我用於將文件.name寫入路徑爲/sdcard/的文件夾中的代碼,其名稱以cimage開頭。目前我有4個這樣的文件夾,其中2個是最近創建的。問題在於這兩個,並影響我以後手動添加的任何文件夾。寫入文件時出現Mysterious IOException

public static void WriteName(ArrayList<String> citem) { 
    final String not_writable = "norw"; 
    for (int i = 0; i < citem.size(); i++) { 
     try { 
      File root = new File(Environment.getExternalStorageDirectory() 
        .getName() + "/" + citem.get(i)); 
      File namefile = new File(root, ".name"); 
      FileReader namereader = new FileReader(namefile); 
      BufferedReader in = new BufferedReader(namereader); 
      String[] str_array = new String[4]; 
      str_array[0] = in.readLine(); 
      str_array[1] = in.readLine(); 
      str_array[2] = in.readLine(); 
      str_array[3] = in.readLine(); 
      Log.d("NameWrite", "line 4: " + str_array[3]); 
      if (str_array[3] == null || !str_array[3].equals(not_writable)) { 
       FileWriter namewriter = new FileWriter(namefile); 
       BufferedWriter out = new BufferedWriter(namewriter); 
       out.write(i 
         + "\nCImage_" 
         + (i) 
         + "\nAutogenerated Stub\nnorw\n"); 
       out.close(); 
      } else { 
       Log.d("NameManager.WriteName", "Skipping " + root 
         + ", norw set"); 
      } 
     } catch (IOException e) { 
      Log.e("NameManager.java : ", ("Error!! Not Writable!!" 
        + Environment.getExternalStorageDirectory().getName() 
        + "/" + citem.get(i))); 
     } 
    } 

} 

那麼會發生什麼情況是,當該方法嘗試寫最後2個文件夾,它拋出一個IOException

02-26 05:42:39.663: D/NameManager.java(5316): Checking for whatever 
02-26 05:42:39.671: D/NameManager.java(5316): SDcard mounted RW 
02-26 05:42:40.694: D/java.lang.java.lang.String(5316): file (43) :cimages 
02-26 05:42:40.694: D/java.lang.java.lang.String(5316): file (76) :cimages_1 
02-26 05:42:40.694: D/java.lang.java.lang.String(5316): file (77) :cimageslkj 
02-26 05:42:40.694: D/java.lang.java.lang.String(5316): file (81) :cimages_2 
02-26 05:42:40.694: D/NameWrite(5316): line 4: norw 
02-26 05:42:40.694: D/NameManager.WriteName(5316): Skipping sdcard/cimages, norw set 
02-26 05:42:40.702: D/NameWrite(5316): line 4: norw 
02-26 05:42:40.702: D/NameManager.WriteName(5316): Skipping sdcard/cimages_1, norw set 
02-26 05:42:40.702: E/NameManager.java :(5316): Error!! Not Writable!!sdcard/cimageslkj 
02-26 05:42:40.710: E/NameManager.java :(5316): Error!! Not Writable!!sdcard/cimages_2 
02-26 05:42:40.725: D/java.lang.java.lang.String(5316): file (77) :cimageslkj 

我能找到此行爲的解釋。所有其他應用程序可以寫入這些文件夾,以防萬一你想知道。什麼可能導致這種情況?

+0

檢查內部異常。 – 2012-02-26 00:26:58

+0

@TomasVoracek如何做到這一點? – 2012-02-26 00:27:51

+0

使用IOException.printStackTrace,或者只是在調試模式下檢查可用的方法/屬性。 http://stackoverflow.com/questions/8217500/how-to-print-stacktrace-for-an-exception-android – 2012-02-26 00:30:51

回答

0

這沒什麼神祕的,我忘了給.name不存在的情況添加循環。添加這個循環修復了它。

........ 
........ 
try { 
       File root = new File(Environment.getExternalStorageDirectory() 
         .getName() + "/" + fsitem.get(i)); 
       File namefile = new File(root, ".name"); 
       if (!namefile.exists()){ 
        namefile.createNewFile(); 
       } 
       FileReader namereader = new FileReader(namefile); 
       BufferedReader in = new BufferedReader(namereader); 
       String[] str_array = new String[4]; 
........ 
........ 
0

問題的很大一部分是這樣的:

 } catch (IOException e) { 
      Log.e("NameManager.java : ", ("Error!! Not Writable!!" 
       + Environment.getExternalStorageDirectory().getName() 
       + "/" + citem.get(i))); 
     } 

你放棄所有的你已經陷入了IOException的信息,包括實際的異常和消息的名稱。扔掉基本的例外信息是不好的做法......而你被燒燬了。

最佳做法是使用Log.e(tag, msg, throwable)過載在日誌中包含例外。

或者,如果這是「用戶錯誤」,則需要診斷問題並將其報告給用戶。爲此,您可能需要捕獲相關子類型IOException(例如FileNotFoundException)並相應地修改您的最終用戶診斷和恢復。

+0

我會記住這一點,謝謝。 – 2012-02-26 05:34:19

相關問題