Results 1 to 4 of 4
- 03-16-2011, 09:12 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 28
- Rep Power
- 0
Difficulty with loading custom files
I was testing out the save system of a game I'm designing and it seems that my program will not load the files I write. I'm not sure how to go about doing this, but my method was as follows:
The variable activemission, which is an instance of the class Mission, contains all of the information that is needed for the player to save their progress and continue where they left off. When it comes time to save, activemission is written to a destination using an ObjectOutputStream:
When it comes time to load the file, it is loaded as an Object which is then cast as a Mission:Java Code:public boolean save(int slot) { try { missiono = new FileOutputStream(missionfiles[slot]); missionwriter = new ObjectOutputStream(missiono); missionwriter.writeObject(activemission); missionwriter.flush(); missionwriter.close(); } catch (IOException e) { System.out.println("Save error!"); e.printStackTrace(); return false; } return true; }
The loadSavedMission() just takes elements of a mission and puts them in memory to begin gameplay, so it isn't important for the issues here.Java Code:public Mission load(int slot) throws ClassNotFoundException { try { missionreader = new ObjectInputStream(missionis[slot]); Mission loaded = (Mission)missionreader.readObject(); loadSavedMission(loaded); return loaded; } catch (IOException e) { System.out.println("Couldn't load mission!"); e.printStackTrace(); } return null; }
When I try to load a mission from a place I verified as having saved to, though, I get this error:
Java Code:[COLOR="Red"] java.io.StreamCorruptedException: invalid stream header: 6E03FC4A [/COLOR]
While I could put in the mission properties as a byte sequence with a mission generated by the program taking the mission file as a byte stream and decoding each byte, this would be time-consuming and I would appreciate it if anyone could tell me of a better solution.
-
...is stated after the try-catch block which means that after it is loaded, it is deleted.Java Code:return null;
Try this instead:
Java Code:boolean loadSuccessful = false; try { ... loadSuccessful = true; } catch { ... } loadSuccessful? return loaded : return null;Last edited by ozzyman; 03-16-2011 at 09:19 PM.
- 03-17-2011, 10:26 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 28
- Rep Power
- 0
Thanks for the advice!
Bad news is I've implemented the changes you suggested, but unfortunately I still get the same error....
-
Actually i've just found out from a google search that this is the problem. Apparently, you can't use an ObjectInputStream to read the output of a printwriter. You've done exactly that: save file with printwriter and attempt to load with objectinputstream.
Source: java.io.StreamCorruptedException: invalid stream header
Similar Threads
-
Problems loading large numbers of files
By davisburns in forum AWT / SwingReplies: 2Last Post: 02-25-2011, 04:13 AM -
Loading files via HTTP
By amilien in forum New To JavaReplies: 5Last Post: 11-03-2009, 08:34 PM -
loading files contained within app jar file
By thorne_ in forum New To JavaReplies: 3Last Post: 05-18-2009, 02:26 PM -
loading flash files in linux
By rajeshang in forum AWT / SwingReplies: 0Last Post: 05-31-2008, 01:59 PM -
External JavaScript files not loading
By sajut in forum New To JavaReplies: 0Last Post: 02-15-2008, 05:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks