Serializable对象序列化与反序列化

来源:互联网 发布:apache服务器显示橙色 编辑:程序博客网 时间:2024/06/06 09:54

Serializable对象序列化与反序列化

/** * Created by xiaxueliang. */public class BiSerializeUtil {    /**     * 序列化对象     *     * @throws IOException     */    public static byte[] serializeObject(Object object) {        ByteArrayOutputStream saos = new ByteArrayOutputStream();        try {            ObjectOutputStream oos = new ObjectOutputStream(saos);            oos.writeObject(object);            oos.flush();            return saos.toByteArray();        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    /**     * 反序列化对象     *     * @throws IOException     * @throws ClassNotFoundException     */    public static Object deserializeObject(byte[] buf) {        ByteArrayInputStream sais = new ByteArrayInputStream(buf);        try {            ObjectInputStream ois = new ObjectInputStream(sais);            return ois.readObject();        } catch (Exception e) {            e.printStackTrace();        }        return null;    }}
原创粉丝点击