zoukankan      html  css  js  c++  java
  • java中对象的序列化和反序列化

    【对象的序列化和反序列化 】
    1.定义:
    序列化--将对象写到一个输出流中。反序列化则是从一个输入流中读取一个对象。类中的成员必须是可序列化的,而且要实现Serializable接口,这样的类的对象才能被序列化和反序列化。这个接口是一个表示型的接口。serialVersionUID是一个串行化类的通用标示符,反串行化就是使用这个标示符确保一个加载的类对应一个可串行化的对象。

    自己指定了serialVersionUID,就可以在序列化后,去添加一个字段,或者方法,而不会影响到后期的还原,还原后的对象照样可以使用,而且还多了方法可以用。serialVersionUID的生成,可以写1,也可以写2,但最好还是按照摘要算法,生成一个惟一的指纹数字,eclipse可以自动生成的,jdk也自带了这个工具。一般写法类似于
    private static final long serialVersionUID = -763618247875550322L;

    2.序列化步骤:
    1)创建一个对象输出流:可以包装一个其他类型的输出流。
    2)通过对象输出流的writeObject()写对象。注意使用类型转化,转换为相应的类的对象。
    3.反序列化步骤:
    1)创建一个对象输入流:可以包装一个其他类型的输出流。
    2)通过对象输出流的readObject()写对象。
    4.什么对象需要被序列化?
    序列化的基本想法是完成对实例信息的保证。因为实例信息在运行结束后就消失了。要储存什么信息呢?一定不是关于方法的信息。

    5.使用注意事项:
    a.有些属性处于安全考虑不能被序列化(或者不能被序列化),则用transit修饰,若想进一步控制序列化和反序列化,则类中提供readObject()方法和writeObject()方法,(反)序列化时就会调用自定义的方法。注意这两个方法不是在java.io.Serializable接口定义的,而是在ObjectInputStream和ObjectOutputStream类中定义的,这两个类实现ObjectInput 和ObjectOutput两个接口。下边是一个可变数组的例子,取自Java in a Nutshell 2rd Edition,它在串行化实现了高效的针对可变数组程度调整的方法:

    public class IntList implements Serializable
    {
    // An array to store the numbers.
    private int[] nums = new int[8];
    // Index of next unused element of nums[].
    private transient int size = 0;
    /** Return an element of the array */
    public int elementAt(int index) throws ArrayIndexOutOfBoundsException {
    if (index >= size) throw new ArrayIndexOutOfBoundsException(index);
    else return nums[index];
    }
    /** Add an int to the array, growing the array if necessary. */
    public void add(int x) {
    // Grow array, if needed.
    if (nums.length == size) resize(nums.length*2);
    // Store the int in it.
    nums[size++] = x;
    }
    /** An internal method to change the allocated size of the array. */
    protected void resize(int newsize) {
    int[] oldnums = nums;
    // Create a new array.
    nums = new int[newsize];
    // Copy array elements.
    System.arraycopy(oldnums, 0, nums, 0, size);
    }
    /** Get rid of unused array elements before serializing the array. */
    private void writeObject(ObjectOutputStream out) throws IOException {
    // Compact the array.
    if (nums.length > size) resize(size);
    // Then write it out normally.
    out.defaultWriteObject();
    }
    /** Compute the transient size field after deserializing the array. */
    private void readObject(ObjectInputStream in)
    throws IOException, ClassNotFoundException {
    // Read the array normally.
    in.defaultReadObject();
    // Restore the transient field.
    size = nums.length;
    }
    }

    b.由于串行化是针对类的实例,也就是对象的,所以static这样的成员是类相关的,也就不会被串行化。
    c.注意,API中一个类一旦被串行化了,那么它的父类都是串行化的。你自己设计的类要是想被串行化,那么其父类要确保都串行化。父类一旦串行化,则子类们都自动实现了串行化。所以内部类和可扩展类不建议串行化。

  • 相关阅读:
    爬取校园新闻首页的新闻的详情,使用正则表达式,函数抽离
    网络爬虫基础练习
    Mysql 使用 select into outfile
    Mysql 使用CMD 登陆
    使用Clean() 去掉由函数自动生成的字符串中的双引号
    Get Resultset from Oracle Stored procedure
    获取引用某个主键的所有外键的表
    Entity Framework 丢失数据链接的绑定,在已绑好的EDMX中提示“Choose Your Data Connection”
    添加MySql Metat Database 信息
    at System.Data.EntityClient.EntityConnection.GetFactory(String providerString)
  • 原文地址:https://www.cnblogs.com/fjhh/p/5370853.html
Copyright © 2011-2022 走看看