Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: saving / loading a file

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default saving / loading a file

    I am looking for some help with saving a file / loading it back up again. I am going to avoid the whole serialization thing, I hope, but simply writing a text file, and reading it back in. This is for a tic tac toe type game and would need to save the state of the game.

    I am not really sure how to make this work, I am hoping for some examples I can be referred to, or some ideas.

    Here are my classes right now. I am able to open and close the fileSelector. I am not really sure how to take all of my variables and all of my arrays and write them out, then recognize them when they're coming back in as a load file to put the game the way it was. I am working on making sure all of these things can be pulled from a single Variables class to make it easy, but I am not sure how to write all these different types of items to a file in a meaningful way, and recognize them when I load it back in.

    Any tips would be awesome.

    package game.cc.gui;
    import game.Variables;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import javax.swing.JFileChooser;
     
    //saveGame class
     
     
    /** items to save to file: 
    *player names (String)
    *scores (int)
    *num pieces remaining (int)
    *button array (JButton)
    *buttonsUsed array (boolean)
    *game size (int)
    *window size (int)
    *1 or 2 player game (int)
    *computer difficulty if 1 player game (int)
    *
    *
    *
    *
    *
    */
    public class Save_Game {
    GUI GUI;
    Variables Variables;
    //Create a file chooser
    final JFileChooser fc = new JFileChooser();
     
     
    //create new save file to be written to disk
    public void save() throws IOException{
    int returnVal = fc.showSaveDialog(fc);
       if (returnVal == JFileChooser.APPROVE_OPTION) {
                 File file = fc.getSelectedFile();
     
    FileWriter fstream = new FileWriter(file);
     
     
    BufferedWriter out = new BufferedWriter(fstream); 
     
    // write Jbutton array to file
    for(int k = 0; k<Variables.getBoardSize()*Variables.getBoardSize(); k++){
    String buttonStatus= GUI.buttons[k].getText();
      try {
    	out.write(buttonStatus);
    } catch (IOException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    }
      try {
    	out.write(" ");
    } catch (IOException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    }
    } // end for 
     
    // write buttonsUsed array to file
    for(int i = 0; i<Variables.getBoardSize()*Variables.getBoardSize(); i++){
    boolean buttonUsed = (GUI.buttonUsed[i]);
    	if(buttonUsed){
      try {
    	out.write(1);
    } catch (IOException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    }
    	} else
    		try {
    			out.write(0);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	try {
    		out.write(" ");
    	} catch (IOException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    } // end for 
     
      try {
    	out.close();
    } catch (IOException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    }
    }
    } // end method
    } // end save game



    package game.cc.gui;
     
    import java.io.File;
     
    import javax.swing.JFileChooser;
     
    //load game class
     
     
    /** items to load from file: 
    *player names
    *scores
    *num pieces remaining
    *state of buttons
    *game size
    *window size
    *1 or 2 player game
    *computer difficulty if 1 player game
    *
    *
    *
    *
    *
    */
    public class Load_Game{
     
    //Create a file chooser
    	public void load(){
    JFileChooser fc = new JFileChooser();
     
    int returnVal = fc.showOpenDialog(fc);
     
    if (returnVal == JFileChooser.APPROVE_OPTION) {
                 File file = fc.getSelectedFile();
     
    } // end if
     
    } // end load method
    } // end load game


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,441
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: saving / loading a file

    Why do you want to avoid serialization? It honestly will probably make your life a lot easier.

    But I think you're overcomplicating how you're thinking about it. Just write the values out in a specific order, then read them in in that same order. Or you could get a little more advanced and use XML. Or you could just user serialization.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: saving / loading a file

    You can use Properties to read/write settings for your application. It is much easier than serialization.

    Spring 3
    Last edited by cafeteria84; January 22nd, 2012 at 03:02 PM.

Similar Threads

  1. Saving and Loading
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: August 2nd, 2011, 01:31 PM
  2. saving xml file
    By LOL in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 18th, 2010, 05:45 AM
  3. Loading a file in a different folder
    By tiemykim in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2010, 08:53 AM
  4. [SOLVED] Saving A File?
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: August 1st, 2009, 11:56 AM