我正在尝试将对象写入文件并稍后从不同的 Activity 中检索它。 打开输入流以检索对象时,出现 IO 异常:
java.io.FileNotFoundException: /data/data/com.mooney.diveapp/files/savedDiveLocations: open failed: ENOENT (No such file or directory)
字符串和文件初始化:
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= new File(objectConetxActivity.getFilesDir(), fileName);
不会创建您的文件。
您必须调用 theFileName.createNewFile();
。并确保该方法返回 true。这意味着该文件已有效创建。
关于java - 找不到文件将对象保存到电话文件目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25960752/