26
2017
09

Serializable对象序列化与反序列化

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;
    }
}
上一篇:android笔记:android 打包butterknife报错 下一篇:Android Studio使用经验