Java object serialization a tool class saved as an XML file

Source: Internet
Author: User
Tags object serialization serialization

Today I looked at the Java.beans bag and found two good things, Xmlencoder and Xmldecoder. It's hard to find yourself accessing objects from XML before. Do the gadget class, you can use it later.

 The following is a reference fragment: 


package com.imct.util;


import Java.beans.XMLDecoder;


import Java.beans.XMLEncoder;


import Java.io.File;


import Java.io.FileInputStream;


import java.io.FileNotFoundException;


import Java.io.FileOutputStream;


import java.io.IOException;


import java.util.ArrayList;


import java.util.List;


/**


* <title> classes </title>
using XML files to access serializable objects

* <description> provides methods of saving and reading </description>


* @author Yinjin


* <copyright> Institute of Automotive Engineering Development, Tsinghua University @2005</copyright>


* @version 1.0


* 2005-8-5 16:44:49


*/


public class Objecttoxmlutil


{


/**


* Serialize Java Serializable objects (implementing the Serializable interface) to the XML file, and if you want to save multiple serializable objects at once, use the collection for encapsulation


* will be saved with the current object's original XML file content


* @param obj serializable object to serialize


* @param filename with a full save path


* @throws filenotfoundexception the file at the specified location does not exist

An exception occurred
* @throws IOException output


* @throws Exception other run-time exceptions


*/


public static void Objectxmlencoder (Object obj,string fileName)


throws Filenotfoundexception,ioexception,exception


{


//Create output file


file fo = new file (fileName);


//file does not exist, the file is created


if (!fo.exists ())


{


//Create a directory of files first


String Path = filename.substring (0,filename.lastindexof ('. '));


file PFile = new file (path);


Pfile.mkdirs ();


}


//create file output stream


FileOutputStream fos = new FileOutputStream (FO);


//Create XML File object output class instance


Xmlencoder encoder = new Xmlencoder (FOS);


//object serialization output to XML file


encoder.writeobject (obj);


Encoder.flush ();


//Turn off serialization tools


Encoder.close ();


//Turn off output stream


Fos.close ();


}


/**


* Reads the serialized saved object in the XML file specified by Objsource, and returns the result after the list package


* @param objsource full file path with full file


* @return List of objects that are stored in an XML file (possibly one or more serialized objects)


* @throws FileNotFoundException The specified object read resource does not exist


* @throws IOException Read error


* @throws Exception Other run-time exceptions occur


*/


public static List Objectxmldecoder (String objsource)


throws Filenotfoundexception,ioexception,exception


{


List objlist = new ArrayList ();


File fin = new file (Objsource);


FileInputStream fis = new FileInputStream (FIN);


Xmldecoder decoder = new Xmldecoder (FIS);


Object obj = null;


Try


{


while (obj = Decoder.readobject ())!= null)


{


objlist.add (obj);


}


}


catch (Exception e)


{


//TODO auto-generated catch block


}


Fis.close ();


Decoder.close ();


return objlist;


}


}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.