Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Created September 28, 2012 07:00
Show Gist options
  • Save neuro-sys/3798357 to your computer and use it in GitHub Desktop.
Save neuro-sys/3798357 to your computer and use it in GitHub Desktop.
Deep copy Java
public class Deepcopy {
public static Object deepClone(Object value) throws IOException, ClassNotFoundException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(value);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
Object deepCopy = ois.readObject();
return deepCopy;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment