Java序列化与反序列化三种格式存取
作者:网络转载 发布时间:[ 2012/8/14 10:16:17 ] 推荐标签:
@SuppressWarnings("resource")
public void restorePerson(){
try{
FileInputStream fls = new FileInputStream("E:\person.txt");
ObjectInputStream ois = new ObjectInputStream(fls);
Person myPerson = (Person)ois.readObject();
System.out.println(" --------------------- ");
System.out.println("Person --read:");
System.out.println("Name is:"+myPerson.getName());
System.out.println("Age is :"+myPerson.getAge());
}catch(Exception e){
e.printStackTrace();
}
}
}
输出结果:? sr test.serializable.Person I ageL namet Ljava/lang/String;xp t Jay
2、XML格式
//参考:http://www.cnblogs.com/bluesky5304/archive/2010/04/07/1706061.html
public class SerializeXML {
public static void main(String[] args) {
SerializeXML ser = new SerializeXML();
ser.serializeToXml();
ser.deSerializeFromXml();
}
public void serializeToXml(){
Person[] myPersons = new Person[2];
myPersons[0] = new Person("Jay", 24);
myPersons[1] = new Person("Tom", 23);
XStream xStream = new XStream();
xStream.alias("Person", Person.class);
try{
FileOutputStream foStream = new FileOutputStream("E:\persons.xml");
xStream.toXML(myPersons,foStream);
}catch(Exception e){
e.printStackTrace();
}
}
public void deSerializeFromXml(){
XStream xStream = new XStream();
xStream.alias("Person", Person.class);
Person[] myPersons = null;
try{
FileInputStream flStream = new FileInputStream("E:\persons.xml");
myPersons = (Person[])xStream.fromXML(flStream);
if(myPersons!=null){
for(Person person:myPersons){
System.out.println(person.getName());
System.out.println(person.getAge());
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11