zoukankan      html  css  js  c++  java
  • Java对象序列化和返序列化

    public class SerializeUtil {
    
    	 /**
         * 序列化
         * 
         * @param object
         * @return
         */
        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();
            }
            return null;
        }
    
        /**
         * 反序列化
         * 
         * @param bytes
         * @return
         */
        public static Object unserialize(byte[] bytes) {
            ByteArrayInputStream bais = null;
            try {
                // 反序列化
                bais = new ByteArrayInputStream(bytes);
                ObjectInputStream ois = new ObjectInputStream(bais);
                return ois.readObject();
            } catch (Exception e) {
            	System.out.println(e.getMessage());
            }
            return null;
        }
    }
    

      

  • 相关阅读:
    Sublime 设置移动光标快捷键
    Reverse Linked List II
    Reverse Nodes in K-Group
    Sort Colors
    Swap Nodes in Pairs
    Intersection of Two Linked Lists
    Word Break
    Unique Binary Search Tree
    Maximal Square
    Happy Number
  • 原文地址:https://www.cnblogs.com/junge8618/p/8880626.html
Copyright © 2011-2022 走看看