#一、序列化机制 参考文献:
JAVA对象序列化示例: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) { e.printStackTrace(); } finally { try { if (oos != null) oos.close(); if (baos != null) baos.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } @SuppressWarnings("unchecked") public staticT unserialize(byte[] bytes) { ByteArrayInputStream bais = null; ObjectInputStream ois = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ois = new ObjectInputStream(bais); Object obj = ois.readObject(); return (T) obj; } catch (Exception e) { e.printStackTrace(); } finally { try { if (bais != null) bais.close(); if (ois != null) ois.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; }
#二、序列化框架
- java自带的serialize
- xml
- binary
- json
- protobuf
- thrift
- kryo fst