Is this the proper way to save an object?
I need to implement a save/load for a board game. The class that calculates everything is called LinkPuzzle, which consists of two variables indicating length and width, and a two dimensional array.
There are so many ways to save things in java that I'm confused as to which one to use, of course, so far all of my attempts have thrown errors. This one says "uncompilable source code". It's located in the GUI.
I'm trying to save the object "puzzle", I've attached a implements serializable to the non-GUI part. It's not working though =/
Code:
JButton loadButton = new JButton("Load");
loadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) throws IOException, ClassNotFoundException {
ObjectInputStream input = new ObjectInputStream(new FileInputStream("array.dat"));
LinkPuzzle puzzle = (LinkPuzzle)(input.readObject());
//adjust GUI according to puzzle
input.close();
}
});
JButton saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) throws IOException, ClassNotFoundException {
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("array.dat",true));
output.writeObject(puzzle);
output.close();
}
});