Results 1 to 7 of 7
  1. #1
    ryuzog is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default 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();
                        }
    		});

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Does LinkPuzzle extend Object?

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    1. Does LinkPuzzle implement Serializable?

    2. Take a look at java.util.prefs.Preferences.

    db

  5. #5
    ryuzog is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default

    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?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    How are you compiling this?
    Because "uncompilable source code" is pretty menaningless.

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

Similar Threads

  1. How can i save the data Internally(auto save)
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2010, 08:31 PM
  2. while and proper conditions for if
    By Saletra in forum New To Java
    Replies: 11
    Last Post: 08-25-2010, 10:37 AM
  3. proper use of IllegalArgumentException
    By vendetta in forum New To Java
    Replies: 1
    Last Post: 01-16-2010, 07:43 PM
  4. pls expalin me with any proper example
    By javastuden in forum New To Java
    Replies: 1
    Last Post: 11-05-2009, 10:35 AM
  5. Need help. Method won't returning proper value..
    By zlwilly in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 09:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •