How to clone a CharArrayWriter?
In Java 1.5, how can I clone开发者_如何学编程 an instance of java.io.CharArrayWriter?
CharArrayWriter x = new CharArrayWriter(200);
x.write("foo bar bob");
CharArrayWriter y = x.clone(); // Object.clone() is not visible!!
Thanks,
mobiGeekThere is no clone method, but you can use writeTo
method.
CharArrayWriter copy = new CharArrayWriter(x.size());
x.writeTo(copy);
CharArrayWriter
is not cloneable. Depending on your actual requirement you can do similar with:
CharArrayWriter y = new CharArrayWriter();
y.write( x.toCharArray() );
Which is essentially the same thing.
精彩评论