1

我想序列化一個對象並將其存儲在sdcard下我的項目名稱,但我得到FileNotFoundException將序列化對象存儲到SD卡作爲文件導致FileNotFoundException

我的代碼編寫如下:

FileOutputStream fileOutputStream = null; 
ObjectOutputStream objectOutputStream = null; 

File dir = new File(Environment.getExternalStorageDirectory(), FILE_LOCATION + username); 

try { 
    if(!dir.exists()) { 
     dir.mkdirs(); 
    } 
    File file = new File(dir, FILE_NAME); 
    fileOutputStream = new FileOutputStream(file); 
    objectOutputStream = new ObjectOutputStream(fileOutputStream); 
    objectOutputStream.writeObject(formList); 
    objectOutputStream.close(); 
} catch(IOException ioException) { 
    ioException.getMessage(); 
} catch (Exception e) { 
    e.getMessage(); 
} 

,這是什麼問題的原因是什麼?
我在模擬器中運行,我的應用程序在android 3.0中。

+0

您是否已將添加到您的清單中? –

+0

雅我有它... – Mathew

+0

我有錯誤:java.io.FileNotFoundException:/mnt/sdcard/Cache/mathew/empInfo.ser(是一個目錄)在行fileOutputStream =新的FileOutputStream(文件); – Mathew

回答

0

我懷疑你的文件名無效,也許是這樣。在目錄中?或者是文件名稱。

+0

文件名有擴展名.ser,這爲我創建了問題。 – Mathew

+0

只是一個筆記,我試着用.ser擴展名,它似乎是爲我工作。 – mbwasi

0

糾正我,如果我錯了,但是你不必在寫入之前創建文件嗎?

File file = new File(dir, FILE_NAME); 
if (!file.exists()) { 
    file.createNewFile(); 
} 
+0

添加了這一行...但仍然有錯誤:java.io.FileNotFoundException:/mnt/sdcard/Cache/mathew/empInfo.ser(是一個目錄) – Mathew

+0

我的猜測是你之前創建了一個dir,其名稱是現在這個文件。嘗試刪除/mnt/sdcard/Cache/mathew/empInfo.ser,然後重新運行更正後的代碼 –

0

我想分享我的解決方案,因爲我從這個問題上得到了很多來自Stackoverflow的幫助(通過搜索以前的答案)。我的解決方案導致了幾個小時的搜索和拼湊解決方案。我希望它能幫助別人。

這將向外部存儲器寫入和讀取自定義對象的ArrayList。

我有一個類爲我的活動和其他類提供IO。鬧鐘是我的習慣課。

@SuppressWarnings("unchecked") 
public static ArrayList<Alarm> restoreAlarmsFromSDCard(String fileName, 
     Context context) { 

FileInputStream fileInputStream = null; 

ArrayList<Alarm> alarmList = new ArrayList<Alarm>();//Alarm is my custom class 
//Check if External storage is mounted 
if (Environment.getExternalStorageState() != null) { 
File dir = new File(Environment.getExternalStorageDirectory(), 
       "YourAppName/DesiredDirectory"); 

try { 
if (!dir.exists()) { 
Log.v("FileIOService", "No Such Directory Exists"); 
} 
File file = new File(dir, fileName); 
fileInputStream = new FileInputStream(file); 
ObjectInputStream ois = new ObjectInputStream(fileInputStream); 
alarmList = (ArrayList<Alarm>) ois.readObject(); 
ois.close(); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
} else { 
//Do something here to warn user 
} 

return alarmList; 
} 

public static void saveAlarmsToSDCard(String fileName, ArrayList<Alarm>  alarmList,Context context) { 
FileOutputStream fileOutputStream = null; 
ObjectOutputStream objectOutputStream = null; 

if (Environment.getExternalStorageState() != null) { 
File dir = new File(Environment.getExternalStorageDirectory(), 
       "YourAppName/DesiredDirectory"); 

try { 
if (!dir.exists()) { 
dir.mkdirs(); 
} 
File file = new File(dir, fileName); 
fileOutputStream = new FileOutputStream(file); 
objectOutputStream = new ObjectOutputStream(fileOutputStream); 
objectOutputStream.writeObject(alarmList); 
objectOutputStream.close(); 
} catch (IOException ioException) { 
ioException.getMessage(); 
} catch (Exception e) { 
e.getMessage(); 
} 
}else{ 
//Do something to warn user that operation did not succeed 
} 

}