Results 1 to 7 of 7
- 10-08-2010, 04:47 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 32
- Rep Power
- 0
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 =/
Java 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(); } });
- 10-08-2010, 04:51 AM #2
- 10-08-2010, 06:50 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Doesn't every class in Java extend Object even when not explicitly stated?
Ever seen a dog chase its tail? Now that's an infinite loop.
- 10-08-2010, 07:11 AM #4
1. Does LinkPuzzle implement Serializable?
2. Take a look at java.util.prefs.Preferences.
db
- 10-08-2010, 08:34 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 32
- Rep Power
- 0
I thought every class automatically extended Object.
Do such replies mean that my code is correct and that the error lies in some other technicality? I've implemented serializable according to an example.
LinkPuzzle implements java.io.Serializable{
...
}
and in the GUI I just have what I posted above.
LinkPuzzle(){
int m;
int n;
int[][] cells;
}
My constructor.
Maybe it has something to do with my file? I just wrote a text document, called it array.dat, and placed it in the same folder with my .java files.
What does the error "source code uncompilable" mean anyway?
- 10-08-2010, 10:12 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
How are you compiling this?
Because "uncompilable source code" is pretty menaningless.
- 10-10-2010, 02:32 AM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Similar Threads
-
How can i save the data Internally(auto save)
By Rama Koti Reddy in forum AWT / SwingReplies: 2Last Post: 11-01-2010, 08:31 PM -
while and proper conditions for if
By Saletra in forum New To JavaReplies: 11Last Post: 08-25-2010, 10:37 AM -
proper use of IllegalArgumentException
By vendetta in forum New To JavaReplies: 1Last Post: 01-16-2010, 07:43 PM -
pls expalin me with any proper example
By javastuden in forum New To JavaReplies: 1Last Post: 11-05-2009, 10:35 AM -
Need help. Method won't returning proper value..
By zlwilly in forum New To JavaReplies: 2Last Post: 12-02-2008, 09:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks