zoukankan      html  css  js  c++  java
  • 序列化反序列化工具类

    1. public class SerializeUtil {  
    2.     public static byte[] serialize(Object object) {  
    3.         ObjectOutputStream oos = null;  
    4.         ByteArrayOutputStream baos = null;  
    5.         try {  
    6.             // 序列化  
    7.             baos = new ByteArrayOutputStream();  
    8.             oos = new ObjectOutputStream(baos);  
    9.             oos.writeObject(object);  
    10.             byte[] bytes = baos.toByteArray();  
    11.             return bytes;  
    12.         } catch (Exception e) {  
    13.             System.err.println("序列化失败");  
    14.             e.printStackTrace();  
    15.         }  
    16.         return null;  
    17.     }  
    18.   
    19.     public static Object unserialize(byte[] bytes) {  
    20.         ByteArrayInputStream bais = null;  
    21.         try {  
    22.             // 反序列化  
    23.             bais = new ByteArrayInputStream(bytes);  
    24.             ObjectInputStream ois = new ObjectInputStream(bais);  
    25.             return ois.readObject();  
    26.         } catch (Exception e) {  
    27.             System.err.println("反序列化失败");  
    28.             e.printStackTrace();  
    29.         }  
    30.         return null;  
    31.     }  
    32. }  
  • 相关阅读:
    面向对象编程——设计模式之一
    mysql死锁——mysql之四
    Mysql基本类型(字符串类型)——mysql之二
    Mysql基本类型(五种年日期时间类型)——mysql之二
    Mysql基础教程——mysql之一
    JVM启动参数手册——JVM之八
    Thinkphp 框架2
    Thinkphp 框架
    流程(下)
    流程(上)
  • 原文地址:https://www.cnblogs.com/core404/p/6261911.html
Copyright © 2011-2022 走看看