集册 Java实例教程 序列化和取消序列化对象

序列化和取消序列化对象

欢马劈雪     最近更新时间:2020-01-02 10:19:05

557
序列化和反序列化对象



import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;/** N o w  J a v a  . c o m 提 供 **/

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


public class SerializeUtil implements Serializable {


    private static final long serialVersionUID = -8580531504951321703L;


    private static final Logger logger = LoggerFactory/* 来 自 时 代 J a v a 公 众 号*/

            .getLogger(SerializeUtil.class);



    public static byte[] serialize(Object object) {

        ObjectOutputStream oos = null;

        ByteArrayOutputStream baos = null;

        try {

            baos = new ByteArrayOutputStream();

            oos = new ObjectOutputStream(baos);

            oos.writeObject(object);

            byte[] bytes = baos.toByteArray();

            return bytes;

        } catch (Exception e) {

            logger.error("", e);

            return null;

        }

    }


    public static Object unserialize(byte[] bytes) {

        ByteArrayInputStream bais = null;

        try {

            bais = new ByteArrayInputStream(bytes);

            ObjectInputStream ois = new ObjectInputStream(ba
展开阅读全文