Java对象序列化和反序列

package xml;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.UUID;

public class Ser {
    public static void main(String[] args) throws Exception {
        Student student = new Student();
        student.setStuName(UUID.randomUUID().toString());
        student.setId(1);
        student.setStuAge(22);
        byte[] tmp = toByte(student);
        Student student2 = tooT(Student.class, tmp);
        System.out.println(student2.getStuName());
    }

    /**
     * 序列化
     * 
     * @param t
     * @return
     */
    public static <T> byte[] toByte(T t) {
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        ObjectOutputStream o = null;
        try {
            o = new ObjectOutputStream(b);
            o.writeObject(t);
            return b.toByteArray();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (b != null) {
                try {
                    b.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (o != null) {
                    try {
                        o.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;

    }

    /**
     * 反序列
     * 
     * @param clazz
     * @param buffer
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static <T> T tooT(Class<T> clazz, byte[] buffer) throws Exception {
        ByteArrayInputStream i = new ByteArrayInputStream(buffer);
        ObjectInputStream o = null;
        try {
            o = new ObjectInputStream(i);
            return (T) o.readObject();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (i != null) {
                i.close();
            }
            if (o != null) {
                o.close();
            }
        }
        return null;

    }
}

class Student implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 5723299859036666342L;
    private int id;
    private String stuName;
    private int stuAge;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getStuAge() {
        return stuAge;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}
原文地址:https://www.cnblogs.com/mature1021/p/10446267.html