1 public static <T extends Serializable> T clone(T obj) {
2 T cloneObj = null;
3 try {
4 // 写入字节流
5 ByteArrayOutputStream out = new ByteArrayOutputStream();
6 ObjectOutputStream obs = new ObjectOutputStream(out);
7 obs.writeObject(obj);
8 obs.close();
9 // 分配内存,写入原始对象,生成新对象
10 ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
11 ObjectInputStream ois = new ObjectInputStream(ios);
12 // 返回生成的新对象
13 cloneObj = (T) ois.readObject();
14 ois.close();
15 } catch (Exception e) {
16 logger.error("拷贝失败:"+e);
17 }
18 return cloneObj;
19 }