序列化和反序列化

1.序列化其实就是可以将我们的对象以二进制流的形式序列化到磁盘里,反过来,再以反序列化的方式装换成我们的对象。

serialVersionUID:系列化版本UID

简单来说:java是根据两个类的serialVersionUID是否相同来判断是否能进行系列化的,在进行序列化时,JVM会将传过来的serialVersionUID与本地相应实体类的serialVersionUID是否相同一致,如果一致,则可以进行装换,如果不一致,则会报InvalidClassException

 

java.io.InvalidClassException: com.lz.igo.igominiprogram.pojo.query.People; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
	at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:699)
	at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1885)
	at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1751)
	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2042)
	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1573)
	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
	at com.lz.igo.igominiprogram.pojo.query.SerialTest.main(SerialTest.java:27)

具体的序列化过程是这样的:序列化操作的时候系统会把当前类的serialVersionUID写入到序列化文件中,当反序列化时系统会去检测文件中的serialVersionUID,判断它是否与当前类的serialVersionUID一致,如果一致就说明序列化类的版本与当前类版本是一样的,可以反序列化成功,否则失败。

serialVersionUID有两种生成方式:

一是默认的1L,比如:private static final long serialVersionUID = 1L;        
二是根据类名、接口名、成员方法及属性等来生成一个64位的哈希字段,比如:        
private static final  long   serialVersionUID = xxxxL;

 

下面的People类:

 

package com.lz.igo.igominiprogram.pojo.query;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author
 * @描述
 * @date 2019/3/23
 **/

@Getter
@Setter
public class People implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String name;

    private String address;

    private Integer age;
    public People() {
    }

    public People(Long id, String name, String address) {
        this.id = id;
        this.name = name;
        this.address = address;
    }

    @Override
    public String toString() {
        return "People{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                ",age='"+age+'\''+
                '}';
    }
}

 

下面是我们的测试类:

package com.lz.igo.igominiprogram.pojo.query;

import java.io.*;

/**
 * @author
 * @描述
 * @date 2019/3/23
 **/
public class SerialTest {

    public static void main(String[] args) {

        People people = new People(10L,"测试1","测试1",10);
        try {
            //将people对象序列化到磁盘中
            FileOutputStream outputStream = new FileOutputStream("D:/work/people.txt");
            ObjectOutputStream oos = new ObjectOutputStream(outputStream);
            oos.writeObject(people);
            oos.flush();
            oos.close();
            //将磁盘中的对象反序列化成对象

            FileInputStream stream = new FileInputStream("D:/work/people.txt");
            ObjectInputStream inputStream = new ObjectInputStream(stream);
            People people1 = (People) inputStream.readObject();
            System.out.println(people1.toString());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

序列化和反序列化成功。

 

public class Test implements Serializable {

	private static final long serialVersionUID = 1L;

	public static int staticVar = 5;

	public static void main(String[] args) {
		try {
			//初始时staticVar为5
			ObjectOutputStream out = new ObjectOutputStream(
					new FileOutputStream("result.obj"));
			out.writeObject(new Test());
			out.close();

			//序列化后修改为10
			Test.staticVar = 10;

			ObjectInputStream oin = new ObjectInputStream(new FileInputStream(
					"result.obj"));
			Test t = (Test) oin.readObject();
			oin.close();
			
			//再读取,通过t.staticVar打印新的值
			System.out.println(t.staticVar);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

 

最后的输出是 10,对于无法理解的读者认为,打印的 staticVar 是从读取的对象里获得的,应该是保存时的状态才对。其实在静态变量序列化时并不保存,大家也知道静态变量是属于类的状态的,因此 序列化并不保存静态变量。

 

 

 

最后一点:静态变量属于类的状态,是不能被序列化的

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