问题描述
我注意到StringWriter在内部使用StringBuffer.但是,如果您不需要同步开销,是否有等效于在内部使用StringBuilder的StringWriter的功能?
I've noticed that StringWriter uses internally a StringBuffer. But if you don't need that synchronisation overhead is there an equivalent to StringWriter that uses a StringBuilder inside?
推荐答案
如果您碰巧使用Apache Commons IO,则可以重新使用 StringBuilderWriter ,其描述如下:
If you happen to use Apache Commons IO, then you can re-use StringBuilderWriter which is described as follows:
注意:此实现作为java.io.StringWriter的替代方法,提供了非同步的实现(即,用于单线程),以实现更好的性能.为了安全地使用多个线程,应使用java.io.StringWriter.
NOTE: This implementation, as an alternative to java.io.StringWriter, provides an un-synchronized (i.e. for use in a single thread) implementation for better performance. For safe usage with multiple Threads then java.io.StringWriter should be used.
它的实现也很容易实现,因此您可以从中进行仿造并自己实现,而无需添加新的依赖项.
Its implementation is also straightforward as expected, so you can pattern from it and implement it yourself without adding a new dependency.
这篇关于是否有等效于StringWriter但在Java内部具有StringBuilder的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!