Java 对象序列化

目录

一、序列化

定义:

方法:

代码:

二、反序列化

定义:

方法:

代码:

三、自定义类序列化

步骤:

代码:


一、序列化

定义:

将内存中的Java对象保存到磁盘中或通过网络传输过去

方法:

使用过ObjectOutputStream类实现。

另外,ObjectOutputStream和ObjectInputStream不能序列化static和transient的成员变量

代码:

//序列化过程
    @Test
    public void test_Objectoutputstream(){
        ObjectOutputStream oos= null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("D:\\Java develop\\Program\\superior_Java\\hello5.dat"));
            String name="Ingram";
            oos.writeObject(name);
            oos.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(oos!=null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }

二、反序列化

定义:

将对象从磁盘或者网络中读取到内存中(程序中)的过程

方法:

使用ObjectInputStream类实现

代码:

//反序列化过程
    @Test
    public void test_ObjectInputStream()  {
        ObjectInputStream ois= null;
        try {
            ois = new ObjectInputStream(new FileInputStream("D:\\Java develop\\Program\\superior_Java\\hello5.dat"));
            Object s=ois.readObject();
            String str=(String)s;
            System.out.println(str);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            if(ois != null)
                try {
                    ois.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
        }

    }

运行结果:

三、自定义类序列化

步骤:

要实现自定义类序列化:

1.需要去实现接口:Serializable

2.需要为当前类提供一个全局常量:SerializableUID

3.除了当前类Person需要实现Serializable接口之外,还必须保证内部所有属性也必须是可序列化的

代码:

先定义一个Person类

package Network_programming;

import java.io.Serializable;

/*要实现自定义类可序列化:
* 1.需要实现接口:Serializable
* 2.需要当前类提供一个全局常量:SerializableUID
* 3.除了当前类Person需要实现Serializable接口之外,还必须保证内部所有属性也必须是可序列化的
* */
public class Person implements Serializable {
    public static final long serializableUID=23424324234L;
    private  String name;
    private  int number;

    public Person(String name, int number) {
        this.name = name;
        this.number = number;
    }

    public Person() {
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return number
     */
    public int getNumber() {
        return number;
    }

    /**
     * 设置
     * @param number
     */
    public void setNumber(int number) {
        this.number = number;
    }

    public String toString() {
        return "Person{name = " + name + ", number = " + number + "}";
    }
}

序列化与反序列化实现:

package Network_programming;

import org.junit.Test;

import java.io.*;

public class demo2 {
    public static void main(String[] args) {
        test_Objectoutputstream();
        test_ObjectInputStream();
    }

    public static void test_Objectoutputstream(){
        ObjectOutputStream oos= null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("D:\\Java develop\\Program\\superior_Java\\hello6.dat"));
            String name="Ingram";
            //oos.flush();
            oos.writeObject(name);
            oos.writeObject(new Person("ingram",14));
            oos.flush();

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(oos!=null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }

    //反序列化过程
    public static void test_ObjectInputStream()  {
        ObjectInputStream ois= null;
        try {
            ois = new ObjectInputStream(new FileInputStream("D:\\Java develop\\Program\\superior_Java\\hello5.dat"));
            Object s=ois.readObject();
            String str=(String)s;
            System.out.println(str);
            System.out.println((Person)ois.readObject());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            if(ois != null)
                try {
                    ois.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
        }

    }
}

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值