第一次在这里发帖,所以放轻松!
尝试创建读取 omw.data 的方法,检查文件中是否已存在 SavedSession 对象。如果确实如此,则忽略它,否则将其附加到文件的末尾。
我可以添加一个对象,当添加相同的 SavedSession 时,它可以正常工作,说它已经添加了。添加另一种类型的 SavedSession 时,它说它已添加,但是当尝试读取 omw.data 时,它会抛出
java.io.StreamCorruptedException:格式错误:ac
SavedSession 不包含 GeoPoint 对象,而只是使用它来获取纬度和经度。
任何帮助将不胜感激。我确实尝试过搜索并尝试了很多不同的方法,或者并没有真正理解到底是什么问题。
谢谢,如果我遗漏了什么,请告诉我,我会马上补上的。
public void saveDestination(GeoPoint to, String message,
List<String> contactNumbers) throws IOException {
SavedSession newSession = new SavedSession(to, message, contactNumbers);
FileOutputStream fos = c.openFileOutput("omw.data", Context.MODE_APPEND);
fos.close();
FileInputStream fis = c.openFileInput("omw.data");
try {
ObjectInputStream ois = new ObjectInputStream(fis);
List<SavedSession> previousSessions = new LinkedList<SavedSession>();
try {
Object prevSession = null;
while ((prevSession = ois.readObject()) != null) {
previousSessions.add((SavedSession) prevSession);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch(EOFException ex){
System.out.println("End of file reached found: " + previousSessions.size());
}
finally {
System.out.println("ois closed");
ois.close();
}
if (previousSessions.contains(newSession)) {
Toast.makeText(c, "Already contained!", Toast.LENGTH_LONG)
.show();
return;
}
} catch (EOFException ex) {
ex.printStackTrace();
}
fos = c.openFileOutput("omw.data", Context.MODE_APPEND);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(newSession);
oos.flush();
oos.close();
Toast.makeText(c, "Added Item", Toast.LENGTH_LONG).show();
}
错误
03-03 01:56:27.452: W/System.err(3631): java.io.StreamCorruptedException: Wrong format: ac
03-03 01:56:27.452: W/System.err(3631): at java.io.ObjectInputStream.corruptStream(ObjectInputStream.java:830)
03-03 01:56:27.452: W/System.err(3631): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:943)
03-03 01:56:27.462: W/System.err(3631): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2262)
03-03 01:56:27.462: W/System.err(3631): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2217)
03-03 01:56:27.462: W/System.err(3631): at com.mat.omw.fileoperations.FileSaver.saveDestination(FileSaver.java:41)
03-03 01:56:27.462: W/System.err(3631): at com.mat.omw.maps.MapDestination.onOptionsItemSelected(MapDestination.java:284)
03-03 01:56:27.462: W/System.err(3631): at android.app.Activity.onMenuItemSelected(Activity.java:2337)
03-03 01:56:27.462: W/System.err(3631): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:795)
03-03 01:56:27.462: W/System.err(3631): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:160)
03-03 01:56:27.462: W/System.err(3631): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:885)
03-03 01:56:27.462: W/System.err(3631): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:545)
03-03 01:56:27.462: W/System.err(3631): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
03-03 01:56:27.462: W/System.err(3631): at android.view.View$PerformClick.run(View.java:9293)
03-03 01:56:27.462: W/System.err(3631): at android.os.Handler.handleCallback(Handler.java:587)
03-03 01:56:27.462: W/System.err(3631): at android.os.Handler.dispatchMessage(Handler.java:92)
03-03 01:56:27.462: W/System.err(3631): at android.os.Looper.loop(Looper.java:150)
03-03 01:56:27.462: W/System.err(3631): at android.app.ActivityThread.main(ActivityThread.java:4277)
03-03 01:56:27.462: W/System.err(3631): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 01:56:27.462: W/System.err(3631): at java.lang.reflect.Method.invoke(Method.java:507)
03-03 01:56:27.462: W/System.err(3631): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-03 01:56:27.462: W/System.err(3631): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-03 01:56:27.462: W/System.err(3631): at dalvik.system.NativeStart.main(Native Method)
编辑:读这个
发现由于某种原因你不能追加,不太清楚为什么。每次都更改代码以重写数据,但似乎效率很低。