是否有等效于StringWriter但在Java内部具有StringBuilder的东西? [英] Is there an equivalent to StringWriter but with StringBuilder internally in Java?

查看:98
本文介绍了是否有等效于StringWriter但在Java内部具有StringBuilder的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到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,则可以重新使用

If you happen to use Apache Commons IO, then you can re-use StringBuilderWriter which is described as follows:

输出到StringBuilder的编写器实现.

Writer implementation that outputs to a StringBuilder.

注意:此实现作为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的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆