java对象深度克隆的方法:
public static final Object deepClone(Object obj){
if(logger.isDebugEnabled()){
logger.debug("开始深度克隆对象");
}
ByteArrayOutputStream bo = null;
ObjectOutputStream oo = null;
ByteArrayInputStream bi = null;
ObjectInputStream oi = null;
Object resultObject = null;
try {
//将对象写到流里
if(logger.isDebugEnabled()){
logger.debug("将对象写入二进制流");
}
bo=new ByteArrayOutputStream();
oo=new ObjectOutputStream(bo);
oo.writeObject(obj);
//从流里读出来
if(logger.isDebugEnabled()){
logger.debug("在二进制流中将对象读出");
}
bi=new ByteArrayInputStream(bo.toByteArray());
oi=new ObjectInputStream(bi);
resultObject = oi.readObject();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
logger.fatal("没有找到对应的类");
} finally{
if(logger.isDebugEnabled()){
logger.debug("关闭流对象");
}
try {
if(oi != null){
oi.close();
oi = null;
}
if(bi != null){
bi.close();
bi = null;
}
if(oo != null){
oo.close();
oo = null;
}
if(bo != null){
bo.close();
bo = null;
}
} catch (IOException e) {
logger.error("",e);
}
}
return resultObject;
}
欢迎大家拍砖