序列化 来克隆对象
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
package ? ? com.ssal; ?
import
?
?
java.io.ByteArrayInputStream;
import
?
?
java.io.ByteArrayOutputStream;
import
?
?
java.io.ObjectInputStream;
import
?
?
java.io.ObjectOutputStream;
?
public
?
?
classSnippet
{
?
?
public static void main(String[] args) throwsException
{
Integer a =
?
newInteger(1);
ByteArrayOutputStream buf =
?
newByteArrayOutputStream();
ObjectOutputStream o =
?
newObjectOutputStream(buf);
?
o.writeObject(a);
?
?
// Now get copies:
ObjectInputStream in =
?
new ObjectInputStream(newByteArrayInputStream(buf.toByteArray()));
Integer b = (Integer) in.readObject();
System.
?
out.println(a);
a = 12;
System.
?
out.println(b);