Home » Serialize

Serialize

Local Data Storage 7 – Serializing Java Objects to XML and Deserializing XML back to Java Objects

Working with Java and Files Tutorial Trail Read Write XML Files Random Access Files Like object serialization this is very easy and straight forward. Below is two examples of psuedo code for writing and then reading. This code merely shows the steps. We didn’t have to use the buffered streams. With small files you do not have to but with larger files it will speed up reading and writing. XMLEncoder Class //psuedo code does not run import java.beans.*; import java.io.*; public class SerializeToXML { public static void main(String args[]){ // an object, composition of objects or collection of objects AnObject anObject = new AnObject(); XMLEncoder encoder=null; try{ encoder=new XMLEncoder(new BufferedOutputStream(new FileOutputStream("objects.xml"))); }catch(FileNotFoundException fnfe){...

Local Data Storage 5 – Reading and Writing Serialized Objects to a File

Reading and Writing Objects to files or streams is fairly straight forward. Here is a working example. There are a few things to know. Use serialver.exe to create a serial id number for the class. A class must implement Serializable interface and should use @serial tag in Javadoc comments. A cryptic coded file is created which is not very editable or readable. In a later article I will talk about serializing objects to xml streams and files.