对象流ObjectOutpuStream和ObjectInputStream

本文介绍了如何使用Java的序列化(ObjectOutputStream)和反序列化(ObjectInputStream)操作,通过实例展示如何将Student对象存储和恢复。重点讲解了Serializable接口、transient关键字和序列化ID(serialVersionUID)的作用。
摘要由CSDN通过智能技术生成

对象流:使用流传输对象的过程称为序列化和反序列化

  • 序列化:对象存储到存储设备中

  • 反序列化:从存储设备中读取出来


  1. 需要操作文件的流支持

  2. 不可追加,只能覆盖


package com.li.changGe.ioStream.byteStream.objectStream;

import com.li.changGe.pojo.Student;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class ObjectSerializationDemo01 {

  private static String fileName = "C:\\Users\\林木\\Desktop/test.txt";


  public static void main(String[] args) throws Exception{

    objectOutputStreamTest();
    objectInputStreamTest();
  }

//-------------------------------------------------

  public static void objectOutputStreamTest() throws Exception{

    Student student = new Student("长歌", '女');
    Student student1 = new Student("则天", '女');
    Student student2 = new Student("太平", '女');

    Student.height = 170.0;

    ArrayList<Student> students = new ArrayList<>();
    students.add(student);
    students.add(student1);
    students.add(student2);

    //---------------------------------------------------------------

    FileOutputStream fileOutputStream = new FileOutputStream(fileName);

    //不可追加,只能覆盖
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream/*,true*/);
  //--------------------------------------------------------------------------------------------
    /**
     * 将对象写入到文件中去
     *  sr java.util.ArrayListx佉櫱a? I sizexp   w   sr com.li.changGe.pojo.Studentoj{懀? C sexL  changGet Lcom/li/changGe/pojo/ChangGe;L namet Ljava/lang/String;xpYssr com.li.changGe.pojo.ChangGe9d`AJy  xpt 闀挎瓕sq ~ Yssq ~ t 鍒欏ぉsq ~ Yssq ~ t 澶钩x
     */
    objectOutputStream.writeObject(students);

    System.out.println("写入完毕");

    objectOutputStream.close();

  }
//--------------------------------------------------------

  public static void objectInputStreamTest() throws Exception{
    FileInputStream fileInputStream = new FileInputStream(fileName);
    ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

    //只能读取一次
    ArrayList<Student> students = (ArrayList<Student>) objectInputStream.readObject();
    //报错EOFException
    //ArrayList<Student> students1 = (ArrayList<Student>) objectInputStream.readObject();

    System.out.println("反序列化完毕");

    objectInputStream.close();
  //--------------------------------------------------------------------------------------------

    Student.height = 40;
    /**
     * 长歌-女--0.0
     * test()方法里调用height:40.0
     * I'm LiChangGe.
     */
    System.out.println(students.get(0).name+"-"+students.get(0).sex+"-"+"-"+students.get(0).money);

    students.get(0).test();

    students.get(0).changGe.say();

  }

}

对象序列化

  1. 序列化的对象都需要实现Serializable接口

  2. 序列化时对象属性也需要实现Serializable接口


  3. UID用来在序列化和反序列化时确保对象一致的

    UID

    insertUID


  4. transient关键字让序列化时屏蔽这个属性

  5. transient只能修饰变量


  6. 静态属性不会被序列化


package com.li.changGe.pojo;

import java.io.Serializable;

/**
 * 序列化的对象都需要实现Serializable接口
 */
public class Student implements Serializable {

  //UID用来在序列化和反序列化时确保对象一致的
  private static final long serialVersionUID = 8028258403768056093L;

  public String name;
  public char sex;

  //transient让序列化时屏蔽这个属性
  transient public double money = 5.0;

  //静态属性不会被序列化
  public static double height = 5.0;

//----------------------------------------------------
  /**
   * 序列化时对象属性也需要实现Serializable接口
   * 不然会报错:NotSerializableException
   */
  public ChangGe changGe = new ChangGe();

  public Student() {}

  public Student(String name, char sex) {
    this.name = name;
    this.sex = sex;
  }
//-------------------------------------------------------
  /**
   * transient只能修饰变量
   * 静态成员都不会被序列化
   */
  /**
   * 序列化是用来保存和对象有关的信息
   *
   * 静态常量池的东西在类加载时就已经存在了
   * 它是属性类的东西,不属性对象
   */
  /*transient */public static void test(){
    System.out.println("test()方法里调用height:"+height);
  }

}

package com.li.changGe.pojo;

import java.io.Serializable;

public class ChangGe implements Serializable {

  public void say(){
    System.out.println("I'm LiChangGe.");
  }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

helloses

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

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

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

打赏作者

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

抵扣说明:

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

余额充值