First time posting on here so go easy on me!
Trying to create a method that reads in omw.data, checks to see if SavedSession object already exists in the file. If it does then ignore it otherwise append it to the end of the file.
I am able to add one object, when adding the same SavedSession it works correctly saying it's already added. When adding another type of SavedSession it says it is added but when trying to read omw.data it it throws
java.io.StreamCorruptedException: Wrong format: ac
SavedSession doesn't hold a GeoPoint object instead it just uses it to get latitude and longitude.
Any help would be appreciative. I did try searching and tried quite a few different methods or didn't really understand exactly what the problem was.
Thanks, if I'm missing anything let me know and I'll put it right up.
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();
}
Errors
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)
Edit: Read this
Found out you can't append for some reason, not too sure why. Changed code to rewrite data every time but seems highly inefficient.