StringWriter implements the flush method just because its parent class Writer implements the Flushable interface,
If you can see the source code, you will find that StringWriter actually does nothing:
/**
* Flush the stream.
*/
public void flush() {
}
The flush method affects the destination buffer and allows it to be output directly to the destination,
StringWriter operates memory directly and has no destination, so the flush method is meaningless for it.
You can use sw.getBuffer().setLength(0) to achieve this, but it will not reduce the length of the character array stored inside the buffer, it will just write the content as ‘
StringWriter implements the flush method just because its parent class Writer implements the Flushable interface,
If you can see the source code, you will find that StringWriter actually does nothing:
The flush method affects the destination buffer and allows it to be output directly to the destination,
StringWriter operates memory directly and has no destination, so the flush method is meaningless for it.
You can use
sw.getBuffer().setLength(0)
to achieve this, but it will not reduce the length of the character array stored inside the buffer, it will just write the content as‘