這是我用於將文件.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
我能找到此行爲的解釋。所有其他應用程序可以寫入這些文件夾,以防萬一你想知道。什麼可能導致這種情況?
檢查內部異常。 – 2012-02-26 00:26:58
@TomasVoracek如何做到這一點? – 2012-02-26 00:27:51
使用IOException.printStackTrace,或者只是在調試模式下檢查可用的方法/屬性。 http://stackoverflow.com/questions/8217500/how-to-print-stacktrace-for-an-exception-android – 2012-02-26 00:30:51