你如何在Java中“清空”StringWriter?

时间:2021-03-01 23:08:43

What is the correct way to "empty" a StringWriter in Java so that I can reuse the StringWriter without having to create a new one? Neither StringWriter.flush() nor StringWriter.close() seem to have the desired effect.

在Java中“清空”StringWriter的正确方法是什么,以便我可以重用StringWriter而无需创建新的StringWriter? StringWriter.flush()和StringWriter.close()似乎都没有所需的效果。

2 个解决方案

#1


50  

How about calling getBuffer().setLength(0)?

如何调用getBuffer()。setLength(0)?

#2


0  

Alternatively, we could also delete the buffer using the following code:

或者,我们也可以使用以下代码删除缓冲区:

StringBuffer buf = ****.getBuffer();
buf.delete(0,buf.length());

#1


50  

How about calling getBuffer().setLength(0)?

如何调用getBuffer()。setLength(0)?

#2


0  

Alternatively, we could also delete the buffer using the following code:

或者,我们也可以使用以下代码删除缓冲区:

StringBuffer buf = ****.getBuffer();
buf.delete(0,buf.length());