java序列化和反序列化复习

序列化 反序列化:

{

序列化为一般二进制文件:

 

需要序列化的类实现serializable

 

用ObjectOutputStream输出 writeObject()

用ObjectInputStream读入 用readObject()来读出对象

代码:

package org.cc.serialization;

import java.io.Serializable;
import java.util.Date;

public class Person implements Serializable{

	public Person(String name, int age, Date birth, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.birth = birth;
		this.sex = sex;
	}

	private static final long serialVersionUID = 2222L;
	
	private String name;
	private int age;
	private Date birth;
	private String sex;
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getBirth() {
		return birth;
	}

	public void setBirth(Date birth) {
		this.birth = birth;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
 
}



// 输出
import ....;

public class WritePerson {
	public static void main(String[] args) throws Exception{
		File file=new File("src/person.bin");
		FileOutputStream fos=new FileOutputStream(file);
		ObjectOutputStream oos=new ObjectOutputStream(fos);
		
		
		Person person=new Person("cc",13,new Date(),"男");
		oos.writeObject(person);
		
		Person person2=new Person("xx",12,new Date(),"女");
		oos.writeObject(person2);
		
		oos.flush();
		oos.close();
		System.out.println("写入成功");
	}
}


//读入

import ...;

public class ReadPerson {
	public static void main(String[] args) throws Exception{
		File file=new File("src/person.bin");
		FileInputStream fis=new FileInputStream(file);
		ObjectInputStream ois=new ObjectInputStream(fis);
		
		Person p1=(Person)ois.readObject();
		Person p2=(Person)ois.readObject();
		ois.close();
		
		System.out.println(p1.getName()+"  "+p2.getName());
	}
}

 

 

 

 

序列化为XML文件:

XMLEncoder

XMLDecoder

 

 额外的工具:

   xstream 以及依赖包

 

步骤:

  需要序列化的类实现serializable

  xstream.toXML() 写入

  xstream.fromXML()     读出

输出中文乱码的话 可以用:OutputStreamWriter fw=new OutputStreamWriter(new FileOutputStream(file),"UTF-8");

 

// 序列化的类和以上例子一样


import .....
import com.thoughtworks.xstream.XStream;

//写入到xml
public class WritePersonWithXML {
	public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
		
		File file=new File("src/person.xml");
		OutputStreamWriter fw=new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
		
		XStream xs=new XStream();
		xs.alias("person", Person.class);
		xs.aliasField("birthday", Person.class, "birth");
		xs.useAttributeFor(Person.class, "name");
		xs.aliasAttribute("姓名", "name");
//		System.out.println(xs.toXML(new Person("cc2",15,new Date(),"男")));
		xs.toXML(new Person("cc2",15,new Date(),"男"),fw);
	}

}


import ...
import com.thoughtworks.xstream.XStream;

// 从xml中读出
public class ReadPersonWithXML {
	public static void main(String[] args) throws FileNotFoundException {
		
		File file=new File("src/person.xml");
		FileInputStream fis=new FileInputStream(file);
		XStream xs=new XStream();
		xs.alias("person", Person.class);
		xs.aliasField("birthday", Person.class, "birth");
		xs.useAttributeFor(Person.class, "name");
		xs.aliasAttribute("姓名", "name");
		Person p=(Person)xs.fromXML(fis);
		System.out.println(p.getAge());
	}

}

 

 

  序列化控制:

  实现Externalizable接口

   覆写writeExternal

       readExternal

   在序列化和反序列化时会自动调用以上的方法

   把需要序列化的字段手工 out输出(不需要序列化的 不需要此方法输出)   

  这个就不演示了

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值