---
title: "object 的序列化和反序列化"
author: "dmwdmc"
date: "2013-06-15T19:35:38Z"
tags: []
categories: ["编程语言"]
slug: "object"
draft: false
lastmod: "2013-06-15T21:32:51Z"
id: 1887470
url: "https://dmwdmc.iteye.com/blog/1887470"
---

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

```java
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;
	}
	
}
```


然后我们定义

```java
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();
	}
```



最后去测试他们，
先把对象写进文件中，

```java
public static void main(String[] args) {
		try {
			doPojo2File();
					}  catch (Exception e) {
			if(e instanceof EOFException){
				System.out.println("end");
			}else{
			e.printStackTrace();
			}
		}

	}
```


然后把对象读出来，


```java
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自动产生式不同的


如果我们加辆车，

```java
public class Car implements java.io.Serializable{
	private String cardName;

	public String getCardName() {
		return cardName;
	}

	public void setCardName(String cardName) {
		this.cardName = cardName;
	}
}
```



```java
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