第一次在这里张贴,所以对我来说很容易!
尝试创建一个读取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)
修改:Read this
发现你出于某种原因不能追加,不太清楚为什么。改变代码每次都重写数据,但效率非常低。