序列化机制(2)

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

一、序列化算法的描述

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

              输出。

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

二、对序列化算法的理解 

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

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

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

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

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

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

           是输出序列化编号而已。

三、代码: 

1、需要序列化的实体类
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、第一种序列化方式  
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();
  }
 }      

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值