前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java之序列化机制(2)

java之序列化机制(2)

原创
作者头像
IT工作者
发布2022-05-07 14:27:49
4130
发布2022-05-07 14:27:49
举报
文章被收录于专栏:程序技术知识

序列化机制中的序列化算法

一、序列化算法的描述

           1、当程序试图序列化一个对象的时候,程序会先检查该对象是否已经被序列化过,如果已经从未被序列化过,则将此对象序列化成流

              输出。

          2、如果已经被序列化,那么程序直接输出一个序列化编号,而不再重新序列化该对象

二、对序列化算法的理解 

      上面的第二条当对象被程序检查到已经序列化过,就不在序列化此对象,而只是向输出流中输出序列化编号,对这一条的理解是这样的

        1、假设当用一个ObjectOutputStream对象序列化了A对象,也就是把A对象写入了ObjectOutputStream输出流中,如果在用这个

            ObjectOutputStream对象序列化这个A对象,就是把这个A对象写入ObjectOutputStream输出流中,那么只会输出一个序列化编号

       2、假设用一个ObjectOutputStream对象序列化了A对象,也即是把A对象写入了ObjectOutputStream输出流中后;用另一个

           ObjectOutputStream对象序列化A对象,就是把这个A对象写入ObjectOutputStream输出流中,那么就会序列化这个A对象了

       3、所以说对上面那句话的理解应该是:在同一个ObjectOutputStream输出流下,同一个对象只能序列化一次。以后的序列化只

           是输出序列化编号而已。

三、代码: 

1、需要序列化的实体类

代码语言:javascript
复制
public class Person implements Serializable {
     private String name;
     private int age;
  public Person(String name,int age){
      this.name = name;
      this.age= age;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
   this.name = name;
  }
  public int getAge() {
   return age;
  }
  public void setAge(int age) {
   this.age = age;
  }   
}

2、第一种序列化方式  

代码语言:javascript
复制
public static void main(String[] args) {
  Person person = new Person("孙慧星", 12);
  ObjectOutputStream oos = null;
  try {
   oos = new ObjectOutputStream(new FileOutputStream("mutiple.txt"));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  try {
   oos.writeObject(person);
  } catch (IOException e) {
   e.printStackTrace();
  }
  person.setName("孙超兵");
  try {
   oos.writeObject(person);
  } catch (IOException e) {
   e.printStackTrace();
  }
  ObjectInputStream ois = null;
  try {
   ois = new ObjectInputStream(new FileInputStream("mutiple.txt"));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  try {
   Person pp = (Person) ois.readObject();
   System.out.println(pp.getName());
  } catch (IOException e) {
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
 }
3、第二种序列化方式
public static void main(String[] args) {
  Person person = new Person("孙慧星", 12);
  ObjectOutputStream oos = null;
  try {
   oos = new ObjectOutputStream(new FileOutputStream("mutiple.txt"));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  try {
   oos.writeObject(person);
  } catch (IOException e) {
   e.printStackTrace();
  }
  person.setName("孙超兵");
  try {
   oos= new ObjectOutputStream(new FileOutputStream("mutiple.txt"));
  } catch (FileNotFoundException e1) {
   e1.printStackTrace();
  } catch (IOException e1) {
   e1.printStackTrace();
  }
  try {
   oos.writeObject(person);
  } catch (IOException e) {
   e.printStackTrace();
  }
  ObjectInputStream ois = null;
  try {
   ois = new ObjectInputStream(new FileInputStream("mutiple.txt"));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  try {
   Person pp = (Person) ois.readObject();
   System.out.println(pp.getName());
  } catch (IOException e) {
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
 }      

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档