Java object serialization a tool class saved as an XML file

Source: Internet
Author: User
Tags object object serialization return serialization string
Java Object |xml

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.

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 that use XML files to access serializable objects </title>
* <description> provide 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 serializable interfaces) to XML files, and if you want to save multiple serializable objects at once, encapsulate them with a collection
* Save will use the current object's original XML file content
* @param the Serializable object to be serialized by obj
* @param filename with a full save path
* @throws FileNotFoundException The file at the specified location does not exist
* An exception occurred while @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);
The 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 a file output stream
FileOutputStream fos = new FileOutputStream (FO);
Create an XML file object output class instance
Xmlencoder encoder = new Xmlencoder (FOS);
Object serialization output to an XML file
Encoder.writeobject (obj);
Encoder.flush ();
Turn off serialization tools
Encoder.close ();
Turn off the output stream
Fos.close ();
}
/**
* Read the serialized saved object in the XML file specified by Objsource, and the result returned is in the list package
* @param objsource full file name with full file path
* @return List of objects that are saved in an XML file (possibly one or more serialized objects that are saved)
* @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.