【发布时间】:2014-11-15 15:10:43
【问题描述】:
我正在尝试将对象写入文件并稍后从不同的活动中检索它。 打开输入流以检索对象时,出现 IO 异常:
java.io.FileNotFoundException: /data/data/com.mooney.diveapp/files/savedDiveLocations:打开失败: ENOENT(没有这样的文件或目录)
字符串和文件初始化:
String fileName="savedDiveLocations";
File theFileName;
theFileName= new File(objectConetxActivity.getFilesDir(), fileName);
这里的代码从之前保存的文件中返回一个对象:
String fileAddress = objectConetxActivity.getFilesDir()+ fileName;
ObjectInputStream ois = null;
FileInputStream streamIn=null;
Object locationObject = null;
try{
// except thrown here
streamIn = new FileInputStream(fileAddress); // address of file
ois = new ObjectInputStream(streamIn);
locationObject = ois.readObject();
}catch (Exception e){
并将对象写入文件:
ObjectOutputStream oos = null;
FileOutputStream fout = null;
// save to internal durectpry as opposed to exteranl SD card
try{
String fileAddress = objectConetxActivity.getFilesDir()+ fileName;
fout = new FileOutputStream(fileAddress); // filepath
oos = new ObjectOutputStream(fout);
oos.writeObject(mapLocationsObject);
}catch(Exception ex){...
任何意见表示赞赏。
【问题讨论】:
-
您是否尝试检查 theFileName.exists() 是否存在?