Digital Edition

Java Code Stack #7 Long-term Persistence
Java Code Stack #7 Long-term Persistence

In my previous code stacks, I have used ObjectOutputStream extensively for serializing the state of my objects. This is a method I'm comfortable with, and I presume most of us are. But beware! ObjectOutputStream is not the right way to create a persistent copy of an object. When you serialize an object using ObjectOutputStream, all non-static and non-transient members along with the Class definition are pumped out in a binary format. While you can re-create the objects from this binary format with most of the available JVMs, it might not be so, maybe after a decade, when Sun decides to rewrite the Java Class implementation. So how can you save the state of an object in a Java implementation-independent format? Use XMLEncoder.

1. import java.beans.*;
2. import java.util.*;
3. import*;
5. public class enc{
6. public static void main
7. (String ar[]) throws Exception{
9. Hashtable hash=new Hashtable(1,1);
10. hash.put("afj","Moron");
12. FileOutputStream fos=
new FileOutputStream("afj.xml");
13. BufferedOutputStream bos=
new BufferedOutputStream(fos);
16. /** Write a String Object first
17. and then a Hashtable object
18. to an output stream **/
19. XMLEncoder xe=new XMLEncoder(bos);
20. xe.writeObject(new String("Emp. List"));
21. xe.writeObject(hash);
22. xe.close();
25. /** Recreating the objects from
26. the XML File **/
27. FileInputStream fis=
new FileInputStream("afj.xml");
28. BufferedInputStream bis=
new BufferedInputStream(fis);
30. XMLDecoder xd=new XMLDecoder(bis);
31. System.out.println((String)xd.readObject());
33. Hashtable rec=(Hashtable)xd.readObject();
34. System.out.println("afj: "+rec.get("afj"));
36. xd.close();
38. }

When you encode the object as a XML file, output will look something like:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.0_01" class="java.beans.XMLDecoder">
<string>Emp. List</string>
<object class="java.util.Hashtable">
<void method="put">

The XML file created using this method is portable and can be used across different JVM vendors running different runtime versions. Sun claims that XMLEncoder uses a Redundancy Elimination Algorithm for enhanced structural compactness, which means default values of objects are not written to the stream. This is something that you should check out.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

If I change a class after it has been serialized using ObjectOutputStream, there does not seem to be a way to read in the old class using ObjectInputStream. This seems like an easier way if not the only way. Thanks.

Yes you can encode custom objects with reference to other objects. And this a JVM-implementation-independent method of saving the object state. Exactly!

Can this method be used to write complex object hierarchies? I.e. custom objects with references to other objects? Essentially, can this be viewed as a JVM-version-independent method of serialization? Or is it not that robust?

Or just use Oracle TopLink!

java.beans.XMLEncoder and
java.beans.XMLDecoder ---> from 1.4

this code does'nt compile with jdk1.3
I think the XMLEncoder and Decoder are new in 1.4.

Subscribe to the World's Most Powerful Newsletters


"Akvelon is a software development company and we also provide consultancy services to folks who are...
More and more brands have jumped on the IoT bandwagon. We have an excess of wearables – activity tra...
Without lifecycle traceability and visibility across the tool chain, stakeholders from Planning-to-O...
The Jevons Paradox suggests that when technological advances increase efficiency of a resource, it r...
The taxi industry never saw Uber coming. Startups are a threat to incumbents like never before, and ...
The next XaaS is CICDaaS. Why? Because CICD saves developers a huge amount of time. CD is an especia...
One of the biggest challenges with adopting a DevOps mentality is: new applications are easily adapt...
We are seeing a major migration of enterprises applications to the cloud. As cloud and business use ...
HyperConvergence came to market with the objective of being simple, flexible and to help drive down ...
Deep learning has been very successful in social sciences and specially areas where there is a lot o...
In his session at 21st Cloud Expo, James Henry, Co-CEO/CTO of Calgary Scientific Inc., introduced yo...
For better or worse, DevOps has gone mainstream. All doubt was removed when IBM and HP threw up thei...
Your homes and cars can be automated and self-serviced. Why can't your storage? From simply asking q...
Containers are rapidly finding their way into enterprise data centers, but change is difficult. How ...
I think DevOps is now a rambunctious teenager - it's starting to get a mind of its own, wanting to g...
"MobiDev is a software development company and we do complex, custom software development for everyb...
The “Digital Era” is forcing us to engage with new methods to build, operate and maintain applicatio...
Learn how to solve the problem of keeping files in sync between multiple Docker containers. In his ...
Creating replica copies to tolerate a certain number of failures is easy, but very expensive at clou...
"This week we're really focusing on scalability, asset preservation and how do you back up to the cl...