zoukankan      html  css  js  c++  java
  • java序列化深拷贝【转】

    java深拷贝

    序列化和反序列化合成在一起的方法CloneUtils

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.util.HashMap;
    
    /**
     * 深拷贝类
     * @author King
     */
    public class CloneUtils {
        /**
         * 核心深拷贝方法,其它都是测试方法
         * @param obj
         * @author King
         * @return
         */
        @SuppressWarnings("unchecked")
        public static <T extends Serializable> T clone(T obj) {
            T clonedObj = null;
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(obj);
                oos.close();
                ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
                ObjectInputStream ois = new ObjectInputStream(bais);
                clonedObj = (T) ois.readObject();
                ois.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return clonedObj;
        }
    
        
        static class Student implements Serializable{
            String id = "";
            String name = "";
            
            public Student(String id, String name) {
                super();
                this.id = id;
                this.name = name;
            }
            public String getId() {
                return id;
            }
            public void setId(String id) {
                this.id = id;
            }
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            
        }
        
        public static void main(String[] args) {
            HashMap<String ,Student> map1 = new HashMap<String ,Student>();
            Student stu1 = new Student("1","xixi");
            map1.put("stu", stu1);
            HashMap<String ,Student> map2 =  CloneUtils.clone(map1);
            map2.get("stu").setName("bobo");//把新clone的map2中的Student设新值bobo,且它是对象哦
            
            System.out.println(map1.get("stu").getName());//把旧的map1中的Student拿出来发现还是xixi,这说明深拷贝成功
        }
    }

    序列化和反序列化独立方法SerializeUtil

    package com.testdemo.pcis.isc.redis;
    
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.HashMap;
    import java.util.Map;
    /**
     * 序列化对象工具类.
     * 参考网址:http://alanland.iteye.com/blog/1600685
     * @author King
     *
     */
    public class SerializeUtil {
        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 {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
        public static Object unserialize(byte[] bytes) {
            ByteArrayInputStream bais = null;
            ObjectInputStream ois = null;
            try {
                // 反序列化
                bais = new ByteArrayInputStream(bytes);
                ois = new ObjectInputStream(bais);
                return ois.readObject();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("bytes.length"+bytes.length);
                }
            }
            return null;
        }
        
        public static void main(String[] args) {
            Map<String,String> hashMap = new HashMap<String,String>();
            hashMap.put("a","b");
            byte[] bs = SerializeUtil.serialize(hashMap);
            @SuppressWarnings("unchecked")
            Map<String,String> deepCopyMap =(Map<String,String>) SerializeUtil.unserialize(bs);
            System.out.println(deepCopyMap);
        }
    }
  • 相关阅读:
    队列加分项(选作,根据博客质量加1-5分)(补博客)
    队列课下作业(补博客)
    20162319 2017-2018-20162319 《程序设计与数据结构》第7周学习总结
    20162319 补博客——用数组实现循环队列 06.第六周
    20162319 补博客——排序课下作业
    20162319 2017-2018-20162319 《程序设计与数据结构》第5周学习总结
    20162319 2017-2018-20162319 《程序设计与数据结构》第6周学习总结
    20162319 第二学期第一次实验:线性表的应用
    20162314 Experiment 2
    20162314 《Program Design & Data Structures》Learning Summary Of The Seventh Week
  • 原文地址:https://www.cnblogs.com/whatlonelytear/p/5554515.html
Copyright © 2011-2022 走看看