首页 > Java > java类序列化,以及serialVersionUID

java类序列化,以及serialVersionUID

2009年9月27日 zianed 发表评论 阅读评论

对要序列化的类,继承java.io.Serializable接口,给出serialVersionUID的值,如下

private static final long serialVersionUID = 21L;

也即定该值代表了类的版本标识,只有处于同一个版本标识的类才能进行序列化和反序列化。

引用文章中的语句:

Well, serialVersionUID is an exception to the rule that “static fields don’t get serialized”.

在序列化和反序列化的过程中,如果serialVersionUID不一致,则报错:

Exception in thread “main” java.io.InvalidClassException: net.unixcenter.zianed.test.Person; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 21

It checks if the data read from the input stream is compatible with the current definition of the class.

改变serialVersionUID的作用:

1、起到数据加密的作用,防止其他人读取自己序列化的数据;

2、避免过时数据的读取,也就是在增加\修改\移除某些属性时,改变serailVersionUID,防止读出胀数据和不想被读出的数据。

测试代码:

package net.unixcenter.zianed.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

public class TestSerialVersionUID
{
static File file = new File(“security.ser”);

public static void seriallize() throws IOException
{
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
Person p = new Person(“Zianed”, ‘M’,new Date());
oos.writeObject(p);
oos.close();
}

public static void deseriallize() throws IOException,
ClassNotFoundException
{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Person p = (Person) ois.readObject();
System.out.println(p);
}

public static void main(String[] args) throws Exception
{
//seriallize();
deseriallize();
}

}

class Person implements Serializable
{

/**
* <p>Discription:[字段功能描述]</p>
*/
private static final long serialVersionUID = 1L;

private String name;

private char sex;

/**
* <p>Discription:[方法功能中文描述]</p>
* @return Date birth.
*/
public Date getBirth()
{
return birth;
}

/**
* <p>Discription:[方法功能中文描述]</p>
* @param birth The birth to set.
*/
public void setBirth(Date birth)
{
this.birth = birth;
}

private Date birth;

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

/**
* <p>Discription:[方法功能中文描述]</p>
* @return String name.
*/
public String getName()
{
return name;
}

/**
* <p>Discription:[方法功能中文描述]</p>
* @param name The name to set.
*/
public void setName(String name)
{
this.name = name;
}

/**
* <p>Discription:[方法功能中文描述]</p>
* @return char sex.
*/
public char getSex()
{
return sex;
}

/**
* <p>Discription:[方法功能中文描述]</p>
* @param sex The sex to set.
*/
public void setSex(char sex)
{
this.sex = sex;
}

public String toString()
{
return “name->” + name + ” sex->” + sex+” birth->”+birth;
}

}

参考:http://www.javablogging.com/what-is-serialversionuid/

分类: Java 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.

Analytics Plugin created by Jake Ruston's Wordpress Plugins - Powered by Home Electronics and r4 ds.

Digg Plugin created by Jake Ruston's Wordpress Plugins - Powered by Electronics Store and baby gift baskets.