Java中的序列化和反序列化与示例

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)

序列化是一种将对象状态转换为字节流的机制。反序列化是一个相反的过程,其中字节流用于在内存中重新创建实际的Java对象。此机制用于持久化对象。

序列化,反序列化的Java

创建的字节流与平台无关。因此,在一个平台上序列化的对象可以在不同的平台上反序列化。

为了使Java对象可序列化,我们实现了java.io.Serializable接口。
ObjectOutputStream类包含用于序列化Object的writeObject()方法。

public final void writeObject(Object obj)
                       throws IOException

ObjectInputStream类包含用于反序列化对象的readObject()方法。

public final Object readObject()
                  throws IOException,
               ClassNotFoundException

Java中的序列化和反序列化与示例

序列化的优点
1.保存/保持对象的状态。
2.通过网络传播对象。

只能实现这些类的对象,这些对象正在实现java.io.Serializable接口。
Serializable是一个标记接口(没有数据成员和方法)。它用于“标记”java类,以便这些类的对象可以获得某些功能。标记接口的其他示例是: - 可克隆和远程。

需要记住的要点
1.如果父类已实现Serializable接口,则子类不需要实现它,反之亦然。
2.仅通过序列化过程保存非静态数据成员。
3.静态数据成员和临时数据成员不通过序列化过程保存。因此,如果您不想保存非静态数据成员的值,则将其设置为瞬态。
4.反序列化对象时,永远不会调用对象的构造函数。
5.关联对象必须实现Serializable接口。
示例:

class A implements Serializable{

// B also implements Serializable
// interface.
B ob=new B();  
}

SerialVersionUID
序列化运行时将版本号与名为SerialVersionUID的每个Serializable类相关联,在反序列化期间使用该序列化验证序列化对象的发送方和接收方是否已加载与序列化兼容的该对象的类。如果接收方已为对象的类加载了与对应发送方类的UID不同的UID,则反序列化将导致InvalidClassException。Serializable类可以通过声明字段名称来显式声明自己的UID。
它必须是静态的,最终的并且类型为long。
ie- ANY-ACCESS-MODIFIER static final serialVersionUID = 42L;

如果可序列化类没有显式声明serialVersionUID,则序列化运行时将根据类的各个方面为该类计算默认值,如Java对象序列化规范中所述。但是强烈建议所有可序列化类显式声明serialVersionUID值,因为它的计算对类细节高度敏感,这些细节可能因编译器实现而有所不同,类中的任何更改或使用不同的id都可能影响序列化数据。

还建议对UID使用私有修饰符,因为它不能用作继承成员。

serialver
serialver是JDK附带的工具。它用于获取Java类的serialVersionUID编号。
您可以运行以下命令来获取serialVersionUID

serialver [-classpath classpath] [-show] [classname ...]

例1:


// Java code for serialization and deserialization 
// of a Java object
import java.io.*;
  
class Demo implements java.io.Serializable
{
    public int a;
    public String b;
  
    // Default constructor
    public Demo(int a, String b)
    {
        this.a = a;
        this.b = b;
    }
  
}
  
class Test
{
    public static void main(String[] args)
    {   
        Demo object = new Demo(1, "geeksforgeeks");
        String filename = "file.ser";
          
        // Serialization 
        try
        {   
            //Saving of object in a file
            FileOutputStream file = new FileOutputStream(filename);
            ObjectOutputStream out = new ObjectOutputStream(file);
              
            // Method for serialization of object
            out.writeObject(object);
              
            out.close();
            file.close();
              
            System.out.println("Object has been serialized");
  
        }
          
        catch(IOException ex)
        {
            System.out.println("IOException is caught");
        }
  
  
        Demo object1 = null;
  
        // Deserialization
        try
        {   
            // Reading the object from a file
            FileInputStream file = new FileInputStream(filename);
            ObjectInputStream in = new ObjectInputStream(file);
              
            // Method for deserialization of object
            object1 = (Demo)in.readObject();
              
            in.close();
            file.close();
              
            System.out.println("Object has been deserialized ");
            System.out.println("a = " + object1.a);
            System.out.println("b = " + object1.b);
        }
          
        catch(IOException ex)
        {
            System.out.println("IOException is caught");
        }
          
        catch(ClassNotFoundException ex)
        {
            System.out.println("ClassNotFoundException is caught"); 
        }
  
    }
}

输出:

Object has been serialized
Object has been deserialized 
a = 1
b = geeksforgeeks

 

例2:

// Java code for serialization and deserialization 
// of a Java object
import java.io.*;
 
class Emp implements Serializable {
private static final long serialversionUID =
                                 129348938L;
    transient int a;
    static int b;
    String name;
    int age;
 
    // Default constructor
public Emp(String name, int age, int a, int b)
    {
        this.name = name;
        this.age = age;
        this.a = a;
        this.b = b;
    }
 
}
 
public class SerialExample {
public static void printdata(Emp object1)
    {
 
        System.out.println("name = " + object1.name);
        System.out.println("age = " + object1.age);
        System.out.println("a = " + object1.a);
        System.out.println("b = " + object1.b);
    }
 
public static void main(String[] args)
    {
        Emp object = new Emp("ab", 20, 2, 1000);
        String filename = "shubham.txt";
 
        // Serialization
        try {
 
            // Saving of object in a file
            FileOutputStream file = new FileOutputStream
                                           (filename);
            ObjectOutputStream out = new ObjectOutputStream
                                           (file);
 
            // Method for serialization of object
            out.writeObject(object);
 
            out.close();
            file.close();
 
            System.out.println("Object has been serialized\n"
                              + "Data before Deserialization.");
            printdata(object);
 
            // value of static variable changed
            object.b = 2000;
        }
 
        catch (IOException ex) {
            System.out.println("IOException is caught");
        }
 
        object = null;
 
        // Deserialization
        try {
 
            // Reading the object from a file
            FileInputStream file = new FileInputStream
                                         (filename);
            ObjectInputStream in = new ObjectInputStream
                                         (file);
 
            // Method for deserialization of object
            object = (Emp)in.readObject();
 
            in.close();
            file.close();
            System.out.println("Object has been deserialized\n"
                                + "Data after Deserialization."); 
            printdata(object);
 
            // System.out.println("z = " + object1.z);
        }
 
        catch (IOException ex) {
            System.out.println("IOException is caught");
        }
 
        catch (ClassNotFoundException ex) {
            System.out.println("ClassNotFoundException" +
                                " is caught");
        }
    }
}

输出:

Object has been serialized
Data before Deserialization.
name = ab
age = 20
a = 2
b = 1000
Object has been deserialized
Data after Deserialization.
name = ab
age = 20
a = 0
b = 2000
  •   本文标题:Java中的序列化和反序列化与示例 - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/6491.html
    扫描二维码添加微信 
  • ,领取淘宝优惠券,淘宝购物更优惠。现在添加微信,还可以领取机械键盘优惠券!添加微信后,分享淘宝选中的机械键盘给淘宝机器人即可领取!
    支持我们,就用微信淘宝!

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注

    更多阅读