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 ;
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章