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

    Default Unable to create and write files

    I'm trying to create a save game system for a program I'm making. I'm trying to do this by having the player save their progress to a file with the extension .ksm by writing the player's current data of the currently active "Mission" object (containing everything needed to continue the game from where it was left off) to said file using an ObjectOutputStream. While the program starts up fine and error-free, the data doesn't seem to write.

    I double-checked all the directories, and everything was where it should have been, so then I tried commenting out the code that creates a file, manually making one in the directory, and just writing to that. I ran the program, and the file still came up empty. I can't really think of anything else to do, so here's the relevant code:

    Java Code:
    ...
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    ...
    String missiondirectory;
    Mission misalpha;
    File testfile;
    FileOutputStream missionos;
    ObjectOutputStream missionwriter;
    ...
    public MainController() throws IOException {
    
    	missiondirectory = "c:\\tbsmissions";
    	testfile = new File(missiondirectory+"outputtest.ksm");
    ...
    	misalpha = new Mission(testmap1, players);
    ...
    	testfile.createNewFile();
    	missionos = new FileOutputStream(missiondirectory+"outputtest.ksm");
    	missionwriter = new ObjectOutputStream(missionos);
    	missionwriter.writeObject(misalpha);
    }
    So, any obvious problems?

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    how about adding
    Java Code:
    missionWriter.flush();
    missionWriter.close();
    after the writeObject() method call.

    Other ideas include, its possible Windows [7] now has a default security mechanism to simply not let user applications write to the C:\ location. Perhaps try writing to

    new File(System.getProperty("user.home"), "tbsmissions");

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

    Default

    Hmm, nope, no dice. Any other suggestions?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    file still came up empty
    By file empty do you mean it has a length of 0.

    What is the name of the file written to disk?
    Your code suggests this: c:\\tbsmissionsoutputtest.ksm

    What code catches any exception thrown by the Constructor?
    What does it do if it gets an exception?
    Modify the code to use a try{}catch block and add a printStackTrace() to the catch.

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

    Default

    What is the name of the file written to disk?
    Your code suggests this: c:\\tbsmissionsoutputtest.ksm
    Oh wow...that's kind of embarrassing. I changed the "directory" string from "c:\\tbsmissions" to "c:\\tbsmissions\\" and it works now.

    Thanks for the help :D

Similar Threads

  1. Replies: 2
    Last Post: 07-26-2011, 05:53 PM
  2. Replies: 0
    Last Post: 04-12-2010, 12:14 AM
  3. unable to import jar files (crystal report 11)
    By kishore101 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-25-2009, 08:51 AM
  4. Can I write code in 2 different files ?
    By fartek in forum New To Java
    Replies: 2
    Last Post: 02-14-2009, 04:48 PM
  5. Replies: 11
    Last Post: 09-01-2008, 11:56 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
  •