17.XMLDecoder反序列化
0x01 前言
0x02 XMLDecoder介绍
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.*;
public class XMLTest{
// 序列化对象到文件person.xml
public void xmlEncode() throws FileNotFoundException {
Person person = new Person();
person.setAge(18);
person.setName("axin");
XMLEncoder xmlEncoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("person.xml")));
xmlEncoder.writeObject(person);
xmlEncoder.close();
System.out.println("序列化结束!");
}
// 反序列化
public void xmlDecode() throws FileNotFoundException {
XMLDecoder xmlDecoder = new XMLDecoder(new BufferedInputStream(new FileInputStream("person.xml")));
Person person = (Person)xmlDecoder.readObject();
xmlDecoder.close();
person.sayHello();
System.out.println("反序列化成功!");
}
public static void main(String[] args) throws FileNotFoundException {
XMLTest xmlTest = new XMLTest();
xmlTest.xmlEncode();
xmlTest.xmlDecode();
}
}
0x03 利用方法

其他
Last updated