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


Dynatrace is an application performance management software company with products for the informatio...
All in Mobile is a place where we continually maximize their impact by fostering understanding, empa...
CloudEXPO New York 2018, colocated with DevOpsSUMMIT and DXWorldEXPO New York 2018 will be held Nove...
Lori MacVittie is a subject matter expert on emerging technology responsible for outbound evangelism...
Having been in the web hosting industry since 2002, dhosting has gained a great deal of experience w...
A traditional way of software development efforts reimbursing is pay by the hour, which in case of r...
Your job is mostly boring. Many of the IT operations tasks you perform on a day-to-day basis are rep...
The challenges of aggregating data from consumer-oriented devices, such as wearable technologies and...
Headquartered in Plainsboro, NJ, Synametrics Technologies has provided IT professionals and computer...
CI/CD is conceptually straightforward, yet often technically intricate to implement since it require...
Using new techniques of information modeling, indexing, and processing, new cloud-based systems can ...
SYS-CON Events announced today that has been named “Media Sponsor” of SYS-CON's 22n...
Adding public cloud resources to an existing application can be a daunting process. The tools that y...
The now mainstream platform changes stemming from the first Internet boom brought many changes but d...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (No...
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As au...
A valuable conference experience generates new contacts, sales leads, potential strategic partners a...
Andi Mann, Chief Technology Advocate at Splunk, is an accomplished digital business executive with e...
Containers and Kubernetes allow for code portability across on-premise VMs, bare metal, or multiple ...
SYS-CON Events announced today that DatacenterDynamics has been named “Media Sponsor” of SYS-CON's 1...