object 的序列化和反序列化

首先定义pojo. 这里必须要实现implements java.io.Serializable了,否则报
java.io.InvalidClassException: SexyWoman; class invalid for deserialization


public class SexyWoman implements java.io.Serializable{
	private String name;
	private long high;
	private long weight;
	public static String face="pretty";
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public long getHigh() {
		return high;
	}
	public void setHigh(long high) {
		this.high = high;
	}
	public long getWeight() {
		return weight;
	}
	public void setWeight(long weight) {
		this.weight = weight;
	}
	
}


然后我们定义
public static void doPojo2File() throws FileNotFoundException, IOException{
		ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("obj.txt"));
		SexyWoman sw1=new SexyWoman();
		sw1.setName("maggie");
		sw1.setHigh(172);
		sw1.setWeight(65);
		sw1.face="ddd";
		out.writeObject("你坏");
		out.writeObject(new Date());
		out.writeObject(sw1);
//		out.writeInt(25);
		out.close();
	}
	public static void doFile2Pojo() throws FileNotFoundException, IOException, ClassNotFoundException{
		ObjectInputStream in=new ObjectInputStream(new FileInputStream("obj.txt"));
		while(true){
			Object by=(Object)in.readObject();
			if(by==null){
				break;
			}
			System.out.println(by);
			if(by instanceof SexyWoman){
				SexyWoman sw1=(SexyWoman)by;
				System.out.println(sw1.getName());
				System.out.println(sw1.face);
			}
		}
		in.close();
	}



最后去测试他们,
先把对象写进文件中,
public static void main(String[] args) {
		try {
			doPojo2File();
					}  catch (Exception e) {
			if(e instanceof EOFException){
				System.out.println("end");
			}else{
			e.printStackTrace();
			}
		}

	}


然后把对象读出来,

public static void main(String[] args) {
		try {
						doFile2Pojo();
		}  catch (Exception e) {
			if(e instanceof EOFException){
				System.out.println("end");
			}else{
			e.printStackTrace();
			}
		}

	}


读出,我们发现static属性face没有被改变。
引用
你坏
Sat Jun 15 19:33:55 GMT+08:00 2013
SexyWoman@1c43882a
maggie
pretty
end



要注意的是,jdk版本不同,序列化出来的如果是1.5,那么很可能1.4就不能反序列化,所以
我们可以强行定义 serialxxxxxID 在类pojo里面。
否者1.4和1.5的这个ID自动产生式不同的


如果我们加辆车,
public class Car implements java.io.Serializable{
	private String cardName;

	public String getCardName() {
		return cardName;
	}

	public void setCardName(String cardName) {
		this.cardName = cardName;
	}
}


public class SerializeTest {
	public static void main(String[] args) {
		try {
			doPojo2File();
			doFile2Pojo();
		}  catch (Exception e) {
			if(e instanceof EOFException){
				System.out.println("end");
			}else{
			e.printStackTrace();
			}
		}

	}
	public static void doPojo2File() throws FileNotFoundException, IOException{
		ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("obj.txt"));
		SexyWoman sw1=new SexyWoman();
		sw1.setName("maggie");
		sw1.setHigh(172);
		sw1.setWeight(65);
		Car car1=new Car();
		car1.setCardName("ppo");
		sw1.setCar(car1);
		sw1.face="ddd";
		out.writeObject("你坏");
		out.writeObject(new Date());
		out.writeObject(sw1);
//		out.writeInt(25);
		out.close();
	}
	public static void doFile2Pojo() throws FileNotFoundException, IOException, ClassNotFoundException{
		ObjectInputStream in=new ObjectInputStream(new FileInputStream("obj.txt"));
		while(true){
			Object by=(Object)in.readObject();
			if(by==null){
				break;
			}
			System.out.println(by);
			if(by instanceof SexyWoman){
				SexyWoman sw1=(SexyWoman)by;
				System.out.println(sw1.getName());
				System.out.println(sw1.face);
				System.out.println(sw1.getCar().getCardName());
			}
		}
		in.close();
	}
}


输出
引用

你坏
Sat Jun 15 21:31:26 GMT+08:00 2013
SexyWoman@6f967348
maggie
pretty
ppo
end

你可能感兴趣的:(object)