手记

Java 深度拷贝

public Object deepCopy(Object source){

Object destination = new Object() ;

ByteArrayOutputStream bo = null;

ObjectOutputStream oo = null ;

ByteArrayInputStream bi = null ;

ObjectInputStream oi = null ;

try {

//Read from source

bo = new ByteArrayOutputStream();

oo = new ObjectOutputStream(bo);

oo.writeObject(source);

//Write to destination

bi = new ByteArrayInputStream(bo.toByteArray());

oi = new ObjectInputStream(bi);

destination =  (Object) oi.readObject();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

try {

if(bo != null){

bo.close();

}

if(bo != null){

oo.close();

}

if(bo != null){

bi.close();

}

if(bo != null){

oi.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return destination ;

}


0人推荐
随时随地看视频
慕课网APP