Java examples for Reflection:Object
Clone Object via stream
//package com.java2s; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class Main { public static void main(String[] argv) throws Exception { Object obj = "java2s.com"; System.out.println(cloneObject(obj)); }/*from w w w .jav a 2 s . co m*/ /** * Clone Object * @param obj * @return * @throws Exception */ public static Object cloneObject(Object obj) throws Exception { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(byteOut); out.writeObject(obj); ByteArrayInputStream byteIn = new ByteArrayInputStream( byteOut.toByteArray()); ObjectInputStream in = new ObjectInputStream(byteIn); return in.readObject(); } }