您的位置:首页 > 编程语言 > Java开发

java 对象 克隆 clone

2012-06-06 14:42 375 查看
/**
* 克隆
*
* @param <T>
* @param t
* @return
*/
public static final <T> T clone(T t) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(t);
out.close();
ByteArrayInputStream bin = new ByteArrayInputStream(baos
.toByteArray());
ObjectInputStream in = new ObjectInputStream(bin);
Object clone = in.readObject();
in.close();
return (T) (clone);
} catch (ClassNotFoundException e) {
throw new InternalError(e.toString());
} catch (StreamCorruptedException e) {
throw new InternalError(e.toString());
} catch (IOException e) {
throw new InternalError(e.toString());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: