Java中的序列化与反序列化

原创
2020/11/19 17:00
阅读数 572

今天来说一说Java序列化,本博文将以默认序列化与反序列化方法,展示对单一对象对象与List对象的序列化与反序列化的实现。
【单一对象序列化】
Step1.创建需要序列化的类,必须实现Serializable接口的类才可以序列化和反序列化
member.java

package com.example.demo.entity;

import java.io.Serializable;

public class Member implements Serializable {
    private int id;
    private String name;
    private String password;

    public Member(){}
    public Member(int id, String name, String password) {
        super();
        this.id = id;
        this.name = name;
        this.password = password;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "Member [id=" + id + ", name=" + name + ", password=" + password
                + "]";
    }

}

Step2.序列化上面这个类

package com.example.demo.common;

import com.example.demo.entity.Member;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class Test {
    public static void main(String[] args) {
        Member member = new Member(1,"maYun","password");
        try {
            ObjectOutputStream os = new ObjectOutputStream(
                    new FileOutputStream("C:/member.txt"));
            os.writeObject(member); // 将Member对象写进文件
            os.flush();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行完Test.java可以在C盘根目录看到生成的member.txt文件
打开文件是这样的


Step3.反序列化

package com.example.demo.common;

import com.example.demo.entity.Member;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author guiguketang
 * @date 2020/11/6 16:29
 */
public class Test {
    public static void main(String[] args) {

        try {
            ObjectInputStream is = new ObjectInputStream(new FileInputStream("C:/member.txt"));
            Member member = (Member) is.readObject(); // 从流中读取Member的数据
            is.close();
            System.out.println("after Serializable,the member object is: "+member.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}

控制台输出内容>>>

【List对象序列化&反序列化】
java中的序列化与反序列化都要求对象实现Serializable接口(其实就是声明一下),那么List该如何序列化和反序列化呢?
List这种动态改变的集合默认是不实现这个接口的,也就是不能直接序列化。可是数组是可以序列化的,所以我们只需要将List集合与数组进行转换就可以实现序列化与反序列化了,别啰嗦,上代码>>>

package com.example.demo.common;

import com.example.demo.entity.Member;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 硅谷课堂
 * @date 2020/11/6 16:29
 */
public class Test {
    public static void main(String[] args) {

        List<Member> memList = new ArrayList<Member>();
        for(int i=0;i<10;i++){
            Member member = new Member(i,"name"+i,"password"+i);
            memList.add(member);
        }

        Test test = new Test();
        //test.serialList(memList);//执行序列化list方法;
        //执行反序列化List方法
        List <Member>list = test.DeSerialList(new File("c:/members.txt"));
        System.out.println("The DeSerialList is :");
        for(Member memObj:list){
            System.out.println(memObj.toString());
        }

    }

    //序列化List对象
    public <T> void serialList(List<T> list){
        //将List转换成数组
        T[] array = (T[]) list.toArray();
        File file = new File("c:/members.txt");
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file)))
        {
            out.writeObject(array);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    //反序列化List对象
    public <T> List<T> DeSerialList(File file){
        T[] object;
        try(ObjectInputStream out = new ObjectInputStream(new FileInputStream(file)))
        {
            object = (T[]) out.readObject();
            return Arrays.asList(object);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        return null;
    }
}

序列化输出>>>

反序列化输出>>>>


【属性隐藏transient】
如果类的某些属性不希望被序列化,则可以在属性前加transient关键字即可。
example:
 private transient String password;

展开阅读全文
打赏
0
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部