Builder与StringWriter和PrintWriter

Builder与StringWriter和PrintWriter

本文介绍了StringBuilder与StringWriter和PrintWriter的字符串程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个我以前没见过的习语:StringWriter和PrintWriter的字符串汇编。我的意思是,我知道如何使用它们,但我一直使用StringBuilder。是否有一个具体的理由选择一个而不是另一个? StringBuilder方法对我来说似乎更自然,但它只是风格吗?

I recently encountered an idiom I haven't seen before: string assembly by StringWriter and PrintWriter. I mean, I know how to use them, but I've always used StringBuilder. Is there a concrete reason for preferring one over the other? The StringBuilder method seems much more natural to me, but is it just style?

我在这里看了几个问题(包括最近的一个问题:),但没有一个答案实际上解决的问题是,是否有理由更喜欢其他用于简单的字符串汇编。

I've looked at several questions here (including this one which comes closest: StringWriter or StringBuilder ), but none in which the answers actually address the question of whether there's a reason to prefer one over the other for simple string assembly.

这是我见过并使用过很多次的成语:StringBuilder的字符串汇编:

This is the idiom I've seen and used many many times: string assembly by StringBuilder:


    public static String newline = System.getProperty("line.separator");
    public String viaStringBuilder () {
       StringBuilder builder = new StringBuilder();
       builder.append("first thing").append(newline);  // NOTE: avoid doing builder.append("first thing" + newline);
       builder.append("second thing").append(newline);
       // ... several things
       builder.append("last thing").append(newline);
       return builder.toString();
    }

这是StringWriter和PrintWriter的新成语:字符串程序集:

And this is the new idiom: string assembly by StringWriter and PrintWriter:


    public String viaWriters() {
       StringWriter stringWriter = new StringWriter();
       PrintWriter printWriter = new PrintWriter(stringWriter);
       printWriter.println("first thing");
       printWriter.println("second thing");
       // ... several things
       printWriter.println("last thing");
       printWriter.flush();
       printWriter.close();
       return stringWriter.toString();
    }

编辑似乎没有具体有理由偏好另一个,所以我已经接受了最符合我理解的答案,并且+ 1了所有其他答案。另外,我发布了自己的答案,给出了我运行的基准测试结果,以回答其中一个答案。谢谢大家。

Edit It appears that there is no concrete reason to prefer one over the other, so I've accepted the answer which best matches my understanding, and +1ed all the other answers. In addition, I posted an answer of my own, giving the results of the benchmarking I ran, in response to one of the answers. Thanks to all.

再次编辑事实证明,更喜欢一个的具体理由(特别是StringBuilder)。我第一次错过的是添加换行符。当你添加一个换行符(如上所述,作为一个单独的追加)时,它会稍微快一点 - 不是很大,但加上意图的清晰度,它肯定会更好。请参阅下面的答案,了解改进的时间安排。

Edit again It turns out that there is a concrete reason to prefer one (specifically the StringBuilder) over the other. What I missed the first time was the addition of the newline. When you add a newline (as above, as a separate append), it's slightly faster - not hugely, but coupled with the clarity of intent, it's definitely better. See my answer below for the improved timings.

推荐答案

在风格上, StringBuilder 方法更清洁。代码行数较少,并且使用的是专为构建字符串而设计的类。

Stylistically, the StringBuilder approach is cleaner. It is fewer lines of code and is using a class that was specifically designed for the purpose of building strings.

另一个考虑因素是效率更高。回答这个问题的最佳方法是对两种替代方案进行基准测试。但是有一些明确的指针,StringBuilder 应该更快。首先,StringWriter使用引擎盖下的 StringBuffer来保存写入stream的字符。

The other consideration is which is more efficient. The best way to answer that would be to benchmark the two alternatives. But there are some clear pointers that StringBuilder should be faster. For a start, a StringWriter uses a StringBuffer under the hood to hold the characters written to the "stream".

这篇关于StringBuilder与StringWriter和PrintWriter的字符串程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:34