java对象克隆clone

2014-11-24 08:12:24 · 作者: · 浏览: 0

/**
* 克隆
*
* @param
* @param t
* @return
*/
public static final 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());
}
}

摘自 lpdx111的专栏