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