Java序列化和反序列化 Serializable BeanUtils.copyProperties赋值属性方法

2024-10-09 11:07:38 浏览数 (3)

Java序列化和反序列化 Serializable BeanUtils.copyProperties赋值属性方法

代码语言:javascript复制
package com.example.core.mydemo.java;

import com.example.core.mydemo.json2.GsonUtils;
import org.springframework.beans.BeanUtils;

import java.io.*;

/**
 * Java序列化和反序列化 Serializable BeanUtils.copyProperties赋值属性方法
 * Serialization(序列化):将java对象以一连串的字节保存在磁盘文件中的过程,也可以说是保存java对象状态的过程。序列化可以将数据永久保存在磁盘上(通常保存在文件中)。
 *
 * deserialization(反序列化):将保存在磁盘文件中的java字节码重新转换成java对象称为反序列化。
 *
 * output
 *Person object has been serialized.
 * Deserialized Person's name is :{"firstName":"John","lastName":"Doe","sex":"男"}
 * deserializedPerson=com.example.core.mydemo.java.Person@368102c8
 * myPerson=John Doe
 * myPerson 2={"firstName":"John","lastName":"Doe"}
 *
 */
public class SerializationExample {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // 创建要被序列化的对象
        Person person = new Person("John", "Doe");
        person.setSex("男");
        // 创建输出流
        FileOutputStream fileOut = new FileOutputStream("person.ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);

        // 将对象写入输出流
        out.writeObject(person);

        // 关闭输出流
        out.close();
        fileOut.close();

        System.out.println("Person object has been serialized.");

        // 从输入流中读取并反序列化对象
        FileInputStream fileIn = new FileInputStream("person.ser");
        ObjectInputStream in = new ObjectInputStream(fileIn);

        // 从输入流中读取对象
        Person deserializedPerson = (Person)in.readObject();

        // 打印反序列化后的对象信息
        System.out.println("Deserialized Person's name is :"   GsonUtils.toJson(deserializedPerson));

        // 关闭输入流
        in.close();
        fileIn.close();

        //序列化对象赋值
        copy(deserializedPerson);

    }

    private static void copy(Serializable deserializedPerson) {
        System.out.println("deserializedPerson="   deserializedPerson);

        //根据属性值来赋值
        MyPerson myPerson = new MyPerson();
//        Person myPerson = new Person();
        //根据属性值来赋值
        BeanUtils.copyProperties(deserializedPerson,myPerson);
        System.out.println("myPerson="   myPerson.getName());
        System.out.println("myPerson 2="   GsonUtils.toJson(myPerson));

    }

}


package com.example.core.mydemo.java;

import java.io.Serializable;

public class Person implements Serializable {

    private String firstName;
    private String lastName;
    //transient关键字表示有理的,被修饰的数据不能进行序列化
//    private transient String  sex;
    private  String  sex;

    public Person(){
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Person(String fname, String lname){
        this.firstName = fname;
        this.lastName = lname;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName(){
        return firstName;
    }

    public String getLastName(){
        return lastName;
    }

    public String getName(){
        return firstName   " "   lastName;
    }
}

package com.example.core.mydemo.java;

import java.io.Serializable;

public class MyPerson implements Serializable {
    private String firstName;
    private String lastName;

    private String firstName2;
    private String lastName2;

    public MyPerson(){

    }

    public MyPerson(String fname, String lname){
        this.firstName = fname;
        this.lastName = lname;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName2() {
        return firstName2;
    }

    public void setFirstName2(String firstName2) {
        this.firstName2 = firstName2;
    }

    public String getLastName2() {
        return lastName2;
    }

    public void setLastName2(String lastName2) {
        this.lastName2 = lastName2;
    }

    public String getFirstName(){
        return firstName;
    }

    public String getLastName(){
        return lastName;
    }

    public String getName(){
        return firstName   " "   lastName;
    }
}

0 人点赞