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


"MobiDev is a software development company and we do complex, custom software development for everyb...
Enterprises are adopting Kubernetes to accelerate the development and the delivery of cloud-native a...
"Akvelon is a software development company and we also provide consultancy services to folks who are...
In his session at 21st Cloud Expo, Carl J. Levine, Senior Technical Evangelist for NS1, will objecti...
"Codigm is based on the cloud and we are here to explore marketing opportunities in America. Our mis...
The question before companies today is not whether to become intelligent, it’s a question of how and...
High-velocity engineering teams are applying not only continuous delivery processes, but also lesson...
"Cloud Academy is an enterprise training platform for the cloud, specifically public clouds. We offe...
Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to ...
"CA has been doing a lot of things in the area of DevOps. Now we have a complete set of tool sets in...
Agile has finally jumped the technology shark, expanding outside the software world. Enterprises are...
In his session at 21st Cloud Expo, James Henry, Co-CEO/CTO of Calgary Scientific Inc., introduced yo...
"NetApp is known as a data management leader but we do a lot more than just data management on-prem ...
While some developers care passionately about how data centers and clouds are architected, for most,...
Coca-Cola’s Google powered digital signage system lays the groundwork for a more valuable connection...
"We're focused on how to get some of the attributes that you would expect from an Amazon, Azure, Goo...
Data scientists must access high-performance computing resources across a wide-area network. To achi...
"We're developing a software that is based on the cloud environment and we are providing those servi...
SYS-CON Events announced today that has been named “Media Sponsor” of SYS-CON's 22n...
SYS-CON Events announced today that Telecom Reseller has been named “Media Sponsor” of SYS-CON's 22n...