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); } }
感觉空虚寂寞,只是因为你无所关注,无处付出。