如何克隆CharArrayWriter?

时间:2009-12-11 03:27:09

标签: java clone

在Java 1.5中,如何克隆java.io.CharArrayWriter的实例?

CharArrayWriter x = new CharArrayWriter(200);
x.write("foo bar bob");

CharArrayWriter y = x.clone();   //  Object.clone() is not visible!!

谢谢,
mobiGeek

2 个答案:

答案 0 :(得分:4)

没有克隆方法,但您可以使用writeTo方法。

CharArrayWriter copy = new CharArrayWriter(x.size());
x.writeTo(copy);

答案 1 :(得分:0)

CharArrayWriter不可复制。根据您的实际要求,您可以执行以下操作:

CharArrayWriter y = new CharArrayWriter();
y.write( x.toCharArray() );

这基本上是一回事。