开发者

Problem understanding Serializable

开发者 https://www.devze.com 2023-03-22 06:24 出处:网络
just started using the Serializable-thingy, I want to save a couple of lists and some other objects, but I can\'t seem to get some things right.

just started using the Serializable-thingy, I want to save a couple of lists and some other objects, but I can't seem to get some things right.

I have some JLists with their own "DefaultListModel" that I use to sort thing开发者_运维问答s in the list, I call the class SortModel, when I run the program I do this:

SortModel sortModel = new SortModel();
JList jList = new JList(sortModel);

Then later when the program runs, objects are added and sorted according to the specified needs, now, when I save the lists and load them again, they're empty.

I also save a object of a class that holds the background for the program (user chooses one themself), after saving it I need to add it again to the program (the object, background is stored in it), I need to add it to the program again not only load it "as it where", plus I have some objects that I added on that object with their own listeners. After I somehow succeeded in loading it, the objects are there but I can't use them, so I figure listeners don't get saved?

* explaining edit The class that is the program extends JFrame, nothing funny about that I think. The "background obect" (call it map) extends JComponent, I add this to (let's call it program for now...) the program and it pops up with the image which it holds. Onto this map I then add objects that also extends JComponent (call them dots), the dots are assigned their own listeners before they're added, the listeners might not be "real" listeners, but they act the same way, they're "MouseAdapter" does that makes any difference?. /explaining edit *

* code edit * code for saving:

FileOutputStream fOut = new FileOutputStream("testFile.mpd");
ObjectOutputStream outSt = new ObjectOutputStream(fOut);
outSt.writeObject(savedMap);

"testFile.mpd" is what it sounds like, I'm quite sure the .mpd shouldn't matter, you can make up your own formats, right? :) (main-class is called Mappedoodle, .mpd sounds reasonable, no?) "savedMap" is an object of said Mappedoodle and holds all lists and other information needed to be saved.

code for loading:

FileInputStream fIn = new FileInputStream("testFile.mpd");
ObjectInputStream inSt = new ObjectInputStream(fIn);    
Mappedoodle openedMap = (Mappedoodle)inSt.readObject();

The information in openedMap is used (well... it should be...) to overwrite certain things in the program. * /code edit *

Adding everything back onto this object, even adding everything back into the lists wouldn't be so hard since that's just some more lists and a few loops, but I feel like I just don't really get Serializable ^^ so, someone care to try to explain why not everything gets saved? And if it is, why I can't access it? But if I can, how? :)

I don't know what more code should be relevant, please tell me what more information you would need to help me solve this, pasting the whole program would be really messy since it's 11 classes and quite a few lines.

Thanks <3


The code you must show us must be sufficient to demonstrate your error, and I unfortunately must state that yours doesn't. For instance if I use your code in a very simple example (something I recommend that you do), you'll see that it works. My test code:

Mappedoodle.java

import java.io.Serializable;

public class Mappedoodle implements Serializable {
   private static final long serialVersionUID = -1760231235147491826L;
   private String text;
   private int value;

   public Mappedoodle(String text, int value) {
      this.text = text;
      this.value = value;
   }

   public String getText() {
      return text;
   }

   public int getValue() {
      return value;
   }

   @Override
   public String toString() {
      return text + ", " + value;
   }

}

MappedoodleSerialize.java

import java.io.*;

public class MappedoodleSerialize {
   public static void main(String[] args) {
      Mappedoodle savedMap = new Mappedoodle("Fubar", 200);
      FileOutputStream fOut;
      try {
         // your code below
         fOut = new FileOutputStream("testFile.mpd");
         ObjectOutputStream outSt = new ObjectOutputStream(fOut);
         outSt.writeObject(savedMap);
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

MappedoodleDeSerialize.java

import java.io.*;

public class MappedoodleDeSerialize {
   public static void main(String[] args) {
      try {
         // your code below
         FileInputStream fIn = new FileInputStream("testFile.mpd");
         ObjectInputStream inSt = new ObjectInputStream(fIn);
         Mappedoodle openedMap = (Mappedoodle) inSt.readObject();
         System.out.println(openedMap);
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      } finally {
      }
   }
}

This bit of code compiles, runs and outputs as expected. Your error must lie in code that you've not shown us.


This problem was solved 5 years ago, the solution is lost due to bad memory though.

0

精彩评论

暂无评论...
验证码 换一张
取 消