JAVA深复制方法
要使用深复制必须让进行深复制的对象实现Serializable接口,并且保证对象包含的属性对象都实现了Serializable接口
public static Object deepCopy(Object src) {
Object dest = null;
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
dest = in.readObject();
if (null != in) {
in.close();
}
if (null != byteIn) {
byteIn.close();
}
if (null != out) {
out.close();
}
if (null != byteOut) {
byteOut.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return dest;
}