使用输入、输出流:
public static <R,T> T clone(R resouse,T dest){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(resouse);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ois = new ObjectInputStream(bais);
dest = (T) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(null!=oos) {
oos.close();
}
if(null!=ois){
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return dest;
}
也可以使用json把对象转成字符串,然后再反转为对象:
https://mp.csdn.net/postedit/88535528