zoukankan      html  css  js  c++  java
  • Java Serialization

    ref: http://www.studytonight.com/java/serialization-and-deserialization.php

    Serialization and Deserialization in Java

    Serializationis a process of converting an object into sequence of bytes which can be persisted to a disk or database 

    or can be sent through streams. The reverse process of creating object from sequence of bytes is called deserialization

    A class must implement Serializable interface present in java,io package in order to serialize its object successfully. 

    Serializable is a marker interface that adds serializable behavior to the class implementing it.

    Java provides Serializable API encapsulated under java.io package for serializing and deserializing objects which includes

    • java.io.serializable
    • java.ioExternalizable
    • ObjectInputStream
    • ObjectOutputStream

    Marker interface

    Marker Interface is a special interface in Java without any field and method. Marker interface is used to inform compiler that the class implementing it has some special behaviour or meaning. Some example of Marker interface are,

    • java.io.Serializable
    • java.lang.Cloneable
    • java.rmi.Remote
    • java.util.RandomAccess

    All these interfaces does not have any method and field. They only add special behavior to the classes implementing them. However marker interfaces have been deprecated since Java 5, they were replaced by Annotations. Annotations are used in place of Marker Interface that play the exact same role as marker interfaces did before.

    Signature of writeObject() and readObject()

    writeObject() method of ObjectOutputStream class serializes an object and send it to the output stream.

    public final void writeObject(object x) throws IOException

    readObject() method of ObjectInputStream class references object out of stream and deserialize it.

    public final Object readObject() throws IOException,ClassNotFoundException

    while serializingwhile serializing if you do not want any field to be part of object state then declare it either static or transient based on your need and it will not be included during java serialization process

    Serializing an Object

    import java.io.*;
    class studentinfo implements Serializable 
    {
     String name;
     int rid;
     static String contact;
     studentinfo(string n, int r, string c)
     {
      this.name = n;
      this.rid = r;
      this.contact = c;
     }
    }
    
    class Test
    {
     public static void main(String[] args)
     {
     try
     {
      Studentinfo si = new studentinfo("Abhi", 104, "110044");
      FileOutputStream fos = new FileOutputStream("student.ser");    // student.ser is the file name
      ObjectOutputstream oos = new ObjectOutputStream(fos);
      oos.writeObject(si);
      oos.close();
      fos.close();
      }
      catch (Exception e)
      { e. printStackTrace(); }
     }
    }

    Object of Studentinfo class is serialized using writeObject() method and written to student.ser file.

    Deserialization of Object

    import java.io * ;
    class DeserializationTest
    {
     public static void main(String[] args)
     {
      studentinfo si=null ;
      try  
      {
       FileInputStream fis = new FileInputStream("student.ser");
       ObjectInputStream ois = new ObjectInputStream(fis);
       si = (studentinfo)ois.readObject();
      } 
      catch (Exception e)
       { e.printStackTrace(); }
      System.out.println(si.name);
      System.out. println(si.rid);
      System.out.println(si.contact);
     }
    }

    transient Keyword

    While serializing an object, if we don't want certain data member of the object to be serialized we can mention it transient. transient keyword will prevent that data member from being serialized.

    class studentinfo implements Serializable 
    {
     String name;
     transient int rid;
     static String contact;
    }
    
    • Making a data member transient will prevent its serialization.
    • In this example rid will not be serialized because it is transient, and contact will also remain unserialized because it is static.
  • 相关阅读:
    [CareerCup] 4.6 Find Next Node in a BST 寻找二叉搜索树中下一个节点
    Android 接入支付宝支付实现
    Android 设置软键盘搜索键以及监听搜索键点击事件
    Android 应用监听自身卸载,弹出用户反馈调查
    ndk制作so库,ndk-build不是内部或外部命令。。。的错误
    Error: Your project contains C++ files but it is not using a supported native build system
    Android开发之——依赖冲突Program type already present
    基于Python的开源人脸识别库:离线识别率高达99.38%
    Android5.0以后,materialDesign风格的加阴影和裁剪效果
    Android 5.0 以上监听网络变化
  • 原文地址:https://www.cnblogs.com/morningdew/p/5620001.html
Copyright © 2011-2022 走看看