| Author |
Best way to serialize an object?
|
Mike Lipay
Ranch Hand
Joined: Sep 11, 2007
Posts: 171
|
|
I have an object (see below) that I want to write to a file. The object resides in an array, so I need to dump the entire array to a file, the read it back in in another application.
Thanks.
(Edited to make less wide -- PC)
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3036
|
|
|
You should look up Serialization and Externalization. There is a lot to cover, and correspondingly a lot of articles about it on the internet. You might consider starting with this one.
|
Steve
|
 |
Muhammad Khojaye
Ranch Hand
Joined: Apr 12, 2009
Posts: 341
|
|
|
See some Serialization Examples here.
|
http://muhammadkhojaye.blogspot.com/
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
|
I trust you are not putting the password into a file in "real life"? That would be a serious security risk.
|
 |
Mike Lipay
Ranch Hand
Joined: Sep 11, 2007
Posts: 171
|
|
Ritchie,
Normally I wouldn't (I have encryption routines), but this is a personal application that will not see the light of day past my own use.
|
 |
Mike Lipay
Ranch Hand
Joined: Sep 11, 2007
Posts: 171
|
|
Not sure what's happening here, I'm getting an error I don't understand when reading the object (creating it worked well).
Main class:
Application class:
Here's the run time error:
(Edited to make code less wide -- PC)
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Mike:
What's happening is that you are not assigning a serialVersionUID to the Application class, and so one is being autogenerated by Java. However, you're getting a new one each time you run your program, so they're going to be incompatible. You should create a serialVersionUID for each class that you serialize. If you're using an IDE, it probably has a tool for doing so. Otherwise, you can use serialver.
John.
|
 |
Mike Lipay
Ranch Hand
Joined: Sep 11, 2007
Posts: 171
|
|
John,
I looked at all of the examples in prior posts, nothing seems to state what to do with the serial number once I get it. In fact, none of the ones I looked at mention needing a serial number at all. Do you have anything that shows what to do with the serial number?
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Mike:
You add a static private member like this to each class that you serialize:
I haven't used the serialver tool, but Eclipse has a way to generate the serialVersionUID automatically.
John.
|
 |
Mike Lipay
Ranch Hand
Joined: Sep 11, 2007
Posts: 171
|
|
It's still not working, I entered the command and here is the return:
I added that to the class (maybe in the wrong place?)
But I still receive the same error:
(Edited to make less wide -- PC)
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Mike:
Are you trying to retrieve objects that were serialized before you made the code change? If so, just delete them, and rerun the code to serialize them.
John.
|
 |
Mike Lipay
Ranch Hand
Joined: Sep 11, 2007
Posts: 171
|
|
So, the serialization is created when the file is created?
Is there a way to write an object without having to serialize? That is all that I am trying to do.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
[i may wrong]infact what ever you do with Externalization,you can also do with serialization with overridding of writeObject and readObject right?.please explain me in details if i am wrong
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
"Serialize" is a standard term which means to convert an object from an internal form (in-memory) to an external form (on disk or in a database and so on).
So no, what you want to do IS to serialize the object. But that doesn't mean you have to use this particular kind of serialization. There are many others. For example you could invent your own format where you wrote the properties of the objects to a file in such a way that you could read it back and recreate the object. Or you could use one of the XML serialization tools like java.beans.XMLEncoder. Or you could use an object-relational manager (ORM) like Hibernate and store your objects in a relational database. I'm sure there are other possibilities too.
Of course none of these is the "best way". What's best depends on a real set of requirements, which you don't have yet, given that you're just starting out.
Note also that every one of these serialization tools will have problems if you write out an object and then change the definition of the object (by adding new properties, let's say) and then try to read the serialized object back into a new object with the new definition. That's always something which you need to address when implementing serialization of any kind.
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Mike:
That's what serialization is - writing an object to a file in a retrievable manner. The serialVersionUID is used to determine whether the serialized object matches the class that you're trying to deserialize it into. If you serialize a class with one serialVersionUID, and then change the class' serialVersionUID, when you try and deserialize the object, Java will check the two serialVersionUIDs, and if they don't match, it will throw the Exception that you're seeing.
John.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
|
Getting too difficult a question for "beginning". Moving thread.
|
 |
Mike Lipay
Ranch Hand
Joined: Sep 11, 2007
Posts: 171
|
|
Ok, I thought I got it working, serial number and all. But I made one change to the code and I get the ID error again, if I remove the code then the error goes away. Here's the working code:
Here's the code that doesn't work:
The only change I made was to replace the "currentRecord == 0" with "!fileOpen" (a boolean test).
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
When something "doesn't work", the first thing to do is to look at the error messages that were produced.
At least, I'm assuming there were some. If there were, then you have the luxury of being able to read them but we don't. So we are reduced to guessing, which is rather pointless. But you could remedy that problem.
|
 |
Mike Lipay
Ranch Hand
Joined: Sep 11, 2007
Posts: 171
|
|
Sorry, thought I had added it.
Here's the top of the code too, must have missed it as well:
(Edited to make less wide -- PC)
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Mike:
Each time you make a change to your class, you'll need to delete the previously saved serializations, and probably generate a new serialVersionUID.
John.
|
 |
Mike Lipay
Ranch Hand
Joined: Sep 11, 2007
Posts: 171
|
|
|
That's not entirely true, I've made many changes since I got it working. This is the first change that requested a new ID. If the ID is in a static field, and I have to request a new ID each time before I compile it, that's a real PIA. Hardly makes it worth the effort. Isn't there another way to save an object that doesn't require a serial ID?
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Mike:
That should have been "make a change to the member variables", my bad. Sorry, but if you make those kind of changes, any system is going to say "This serialized object doesn't match that specified by the ID, so I need to throw an exception". You have to have some kind of system to check whether the object you're deserializing matches the class. One thing you can do while you're testing changes is to mark the member as transient, which will cause the member to not be saved when serialized, and have it be defaulted to null/0/false, depending on the type.
John.
|
 |
Mike Lipay
Ranch Hand
Joined: Sep 11, 2007
Posts: 171
|
|
How do I indicate that it is transient?
I coded the logic to create the file, now I am working on the different access routines. I would have thought, once created, that the serial ID would be valid for any program trying to read the file. From what I gather from you, I am going to have to rewrite the file each time I want to read it just so the serial ID is repopulated on the file.
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Mike:
Use the transient keyword.
You only have to change the serialVersionUID if you add or remove member variables - methods and static members aren't taken into account. You can (and should) mark transient those members that don't make sense to save, like a Stream or Writer member.
John.
|
 |
Mike Lipay
Ranch Hand
Joined: Sep 11, 2007
Posts: 171
|
|
Ok, I'm in a catch-22 scenario:
I added a "find" method that calls the "load" method. Java now wants a new serial number. Here's the catch-22: I can't recreate the file because I can't read the file in order to re-write it. If I can't recreate the file then the serial number isn't updated and I can't read it.
Obviously, adding a method does require obtaining a new serial number. Attached is the code, if it helps.
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Mike:
You're right, the serialVersionUID is based partially on the methods (here's the section of the spec on the serialVersionUID), but the methods themselves are not stored in the serialized version.
I guess my suggestion would be that you make your Application class a bean (with just getters and setters), and move some of your adding and finding methods to another class. This will reduce the number of times you need to change the serialVersionUID.
John.
|
 |
Raj chiru
Ranch Hand
Joined: Aug 12, 2008
Posts: 140
|
|
|
serialization with reflection is the best way
|
 |
 |
|
|
subject: Best way to serialize an object?
|
|
|