Java序列化和反序列化

1.为什么要用序列化?   

    1.   在数据传输的时候,对象需要以二进制的字节流的形式来进行传送。

    2.  如果对象长时间没有调用,那么可以通过序列化的方式来将对象存储在本地磁盘上,需要的时候再反序列化,这样可以节省服务器的资源。

 

2.  代码实现序列化

    建立实体类需要实现 Serializable接口,并添加唯一的版本号标识。

    实体类 student: 

package com.test.day02;

import java.io.Serializable;

public class Student implements Serializable  {
	
	
	
	/**
	 * 唯一标识
	 */
	private static final long serialVersionUID = 1L;
	
	String name;
	int age;
	String address;
    Student(String name){
    	this(name,0,null);
    	
    }
    public Student(String name,int age){
    	this(name,age,null);
    	
    }
    Student(String name,int age,String address)
    {
     this.name=name;
     this.age=age;
     this.address=address;
    }
    void study(){
    	System.out.println(name+"在学习。。。。。");
    }
    void sayHai(){
    	System.out.println("我叫"+name+"我今年"+age+"岁了,我家住在"+address);
    }
    
    
}

测试类: 

     序列化使用ObjectOutputStream的os.writeObject(T t), 将对象转换为二进制字节流,反序列化使用ObjectInputStream的is.readObject(),将二进制字节流转换为对象,反序列化时返回的是Object类型,因此需要强制类型转换为对应的对象类型。

package com.test.day05;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.test.day02.Student;

public class Demo10 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Student stu1 = new Student("a",12);

        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        System.out.println(stu1);
        try {
        	File file1=new File("D:/test/stu.txt");
        	if (!file1.exists()) {
        		file1.createNewFile();
			}
            oos = new ObjectOutputStream(new FileOutputStream(file1));
            oos.writeObject(stu1);
            oos.writeObject(stu1);
            
            ois = new ObjectInputStream(new FileInputStream(file1));
            Student stu2 = (Student) ois.readObject();
            Student stu3 = (Student) ois.readObject();
            //Student stu4 = (Student) ois.readObject();
            System.out.println(stu2);
            System.out.println(stu3);
            //读取出来的时候也是从前往后按照顺序读取。
            //System.out.println(stu4);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            if (oos != null) {
                oos.close();
            }
            if(ois!= null){
                ois.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

打印结果:

com.test.day02.Student@6d06d69c
com.test.day02.Student@3b07d329
com.test.day02.Student@3b07d329

由打印结果可知: 序列化后的重新生成了一个新的地址。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乌托邦钢铁侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值