Serializable序列化 && 坑

//大量的反射,创建很多的临时变量,导致内存碎片,Serializable缺点
/**
* 子类实现序列化,父类没有实现序列化,会报错
*/
package com.anny.protobuf.serializable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by zhanghongyan on 2020/4/6.
 */
public class SerializableUtils {
    public static <T> byte[] serialize(T t) throws Exception{
        //Anny  a.静态成员变量属于类不属于对象,所以不会参与序列化(对象序列化保存的是对象的“状态”,也就是它的成员变量,因此序列化不会关注静态变量)
        //Anny  b.用transient关键字标记的成员变量不参与序列化(在被反序列化后,transient 变量的值被设为初始值,如 int 型的是 0,对象型的是 null)
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(t);
        return out.toByteArray();
    }

    public static <T> T deserialize(byte[] bytes) throws Exception{
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
        T t = (T) ois.readObject();
        return t;
    }

    /**
     * 序列化对象
     * @param obj
     * @param path
     * @return
     */
    synchronized public static boolean saveObject(Object obj, String path){
        if (obj == null){
            return false;
        }
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(path));
            oos.writeObject(obj);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 非序列化对象
     * @param path
     * @param <T>
     * @return
     */
    synchronized public static <T> T readObject(String path){
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(path));
            return (T) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
public class Main {
    public static void main(String[] args) throws Exception {
        test01();
    }

    private static void test01() throws Exception {
        Student student = new Student("Anny", "women", 31);
        student.addCourse(new Course("English", 90f));
        student.addCourse(new Course("math", 99f));

        byte[] bytes = SerializableUtils.serialize(student);
        String path = System.getProperty("user.dir") + "\SerializableDemo\src\main\DemoStudent.out";
        SerializableUtils.saveObject(student, path);

        System.out.println(Arrays.toString(bytes));

        System.out.println("=========================================");
        Student student1 = SerializableUtils.deserialize(bytes);
        student1.newDate();
        System.out.println("Student: " + student1);

    }
}
    public static void main(String[] args) throws Exception {
        Course course = new Course("English", 90f);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(outputStream);
        oos.writeObject(course);
        course.setScore(80f);
        oos.reset();
        oos.writeObject(course);
        //oos.writeUnshared(course);//Anny 大坑
        byte[] bytes = outputStream.toByteArray();
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
        Course course1 = (Course)ois.readObject();
        Course course2 = (Course)ois.readObject();
        //Course course3 = (Course)ois.readObject();
        System.out.println("course1: " + course1);
        System.out.println("course2: " + course2);
        //System.out.println("course3: " + course3);
    }
原文地址:https://www.cnblogs.com/anny0920/p/12649712.html