序列化 来克隆对象

package

com.ssal;

import

java.io.ByteArrayInputStream;

import

java.io.ByteArrayOutputStream;

import

java.io.ObjectInputStream;

import

java.io.ObjectOutputStream;

public

class

Snippet

{

public static void main(String[] args) throws

Exception

{

Integer a =

new

Integer(1);

ByteArrayOutputStream buf =

new

ByteArrayOutputStream();

ObjectOutputStream o =

new

ObjectOutputStream(buf);

o.writeObject(a);

// Now get copies:

ObjectInputStream in =

new ObjectInputStream(new

ByteArrayInputStream(buf.toByteArray()));

Integer b = (Integer) in.readObject();

System.

out

.println(a);

a = 12;

System.

out

.println(b);

猜你喜欢

转载自kalllx.iteye.com/blog/1678044