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输出(不需要序列化的 不需要此方法输出)   

  这个就不演示了

}

你可能感兴趣的:(java序列化)