• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best way to serialize an object?  RSS feed

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See some Serialization Examples here.
 
Sheriff
Posts: 57817
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
 
Sheriff
Posts: 23436
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57817
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting too difficult a question for "beginning". Moving thread.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 23436
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
serialization with reflection is the best way
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!