Results 1 to 4 of 4
  1. #1
    DrKilljoy is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default 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:

    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;
    	}
    When it comes time to load the file, it is loaded as an Object which is then cast as a Mission:

    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;
    	}
    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.

    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.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    return null;
    ...is stated after the try-catch block which means that after it is loaded, it is deleted.

    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.

  3. #3
    DrKilljoy is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    Thanks for the advice!

    Bad news is I've implemented the changes you suggested, but unfortunately I still get the same error....

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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

  1. Problems loading large numbers of files
    By davisburns in forum AWT / Swing
    Replies: 2
    Last Post: 02-25-2011, 04:13 AM
  2. Loading files via HTTP
    By amilien in forum New To Java
    Replies: 5
    Last Post: 11-03-2009, 08:34 PM
  3. loading files contained within app jar file
    By thorne_ in forum New To Java
    Replies: 3
    Last Post: 05-18-2009, 02:26 PM
  4. loading flash files in linux
    By rajeshang in forum AWT / Swing
    Replies: 0
    Last Post: 05-31-2008, 01:59 PM
  5. External JavaScript files not loading
    By sajut in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 05:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •