读书人

序列化 回克隆对象

发布时间: 2012-09-24 13:49:41 作者: rapoo

序列化 来克隆对象

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

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);

读书人网 >移动开发

热点推荐