<repo_name>xiangxuan-zhao/practice<file_sep>/manual_practice/src/test/java/com/zxx/serial/Address.java
package com.zxx.serial;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author Administrator
 * @date 2020/04/21
 */
@Data
@AllArgsConstructor
public class Address {
    private String detail;
}
<file_sep>/manual_practice/src/main/java/com/zxx/serial/Male.java
package com.zxx.serial;

import lombok.Data;

/**
 * @author Administrator
 * @date 2020/04/20
 */
@Data
public class Male extends PersonHessian{
    private Long id;
}
<file_sep>/manual_practice/src/test/java/com/zxx/serial/TestJdkSerial.java
package com.zxx.serial;

import com.zxx.utils.JdkSerialUtil;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Administrator
 * @date 2020/04/21
 */
public class TestJdkSerial {
    @Test
    public void testJDKSerialOverwrite() throws IOException, ClassNotFoundException {
        PersonTransit person = new PersonTransit();
        person.setId(1L);
        person.setName("张三");
        person.setMale(true);
        person.setFriends(new ArrayList<PersonTransit>());

        Address address = new Address("某某小区xxx栋yy号");
        person.setAddress(address);
        File file = new File("testJdk.txt");
        // 序列化
        JdkSerialUtil.writeObject(file, person);
        // 反序列化
        PersonTransit personTransit = JdkSerialUtil.readObject(file);
        // 判断是否相等
        Assert.assertEquals(personTransit.getName(), person.getName());
        Assert.assertEquals(personTransit.getAddress().getDetail(), person.getAddress().getDetail());
    }
}
<file_sep>/manual_practice/src/test/java/com/zxx/serial/PersonTransit.java
package com.zxx.serial;

import lombok.Data;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.List;

/**
 * @author Administrator
 * @date 2020/04/21
 */
@Data
public class PersonTransit implements Serializable {
    private Long id;
    private String name;
    private Boolean male;
    private List<PersonTransit> friends;
    private transient Address address;

    /**
     * 自定义序列化写方法
     */
    private void writeObject(ObjectOutputStream oos) throws IOException {
        oos.defaultWriteObject();
        oos.writeObject(address.getDetail());
    }

    /**
     * 自定义序列化读方法
     */
    private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
        ois.defaultReadObject();
        this.setAddress(new Address((String)ois.readObject()));
    }
}
<file_sep>/README.md
# practice
《Java开发手册》
