我什么时候应该使用 Java 的 StringWriter?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27221292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:25:52  来源:igfitidea点击:

When should I use Java's StringWriter?

javastringstringwriter

提问by MarkusWillson

What is a Java StringWriter, and when should I use it?

什么是 Java StringWriter,我应该什么时候使用它?

I have read the documentation and looked here, but I do not understand when I should use it.

我已经阅读了文档并查看了此处,但我不明白什么时候应该使用它。

回答by Jaskey

It is a specialized Writerthat writes characters to a StringBuffer, and then we use method like toString()to get the string result.

它是一种专门的Writer将字符写入 a StringBuffer,然后我们使用 like 方法toString()获取字符串结果的方法。

When StringWriteris used is that you want to write to a string, but the API is expecting a Writeror a Stream. It is a compromised, you use StringWriteronly when you have to, since StringBuffer/StringBuilderto write characters is much more natural and easier,which should be your first choice.

何时StringWriter使用是您想要写入字符串,但 API 需要 aWriter或 a Stream。这是一个折衷的方法,StringWriter只有在必须时才使用,因为StringBuffer/StringBuilder写字符更自然和更容易,这应该是您的首选。

Here is two of a typical good case to use StringWriter

这是两个典型的好用案例 StringWriter

1.Converts the stack trace into String, so that we can log it easily.

1.将堆栈跟踪转换为String,以便我们可以轻松记录它。

StringWriter sw = new StringWriter();//create a StringWriter
PrintWriter pw = new PrintWriter(sw);//create a PrintWriter using this string writer instance
t.printStackTrace(pw);//print the stack trace to the print writer(it wraps the string writer sw)
String s=sw.toString(); // we can now have the stack trace as a string

2.Another case will be when we need to copy from an InputStreamto chars on a Writerso that we can get Stringlater, using Apache commons IOUtils#copy:

2.另一种情况是当我们需要从 aInputStream上的字符复制到 aWriter以便我们String以后可以 使用Apache commons IOUtils#copy 时

StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);//copy the stream into the StringWriter
String result = writer.toString();

回答by RossBille

It is used to construct a string char-by-charor string-by-string.

它用于构造字符串char-by-charstring-by-string.

It is similar to StringBuilderbut uses a StringBufferunder the hood. This is preferable when you are working with an API that requires a stream or writer. If you don't have this requirement it should be more efficient to use a StringBuilder(due to the synchronisation overhead of StringBuffer).

它类似于StringBuilderStringBuffer在引擎盖下使用了一个。当您使用需要流或编写器的 API 时,这是更可取的。如果您没有此要求,则使用 a 应该更有效StringBuilder(由于 的同步开销StringBuffer)。

Of course this only makes sense if you realise that string concatenation (eg.

当然,这只有在您意识到字符串连接(例如

String s = "abc"+"def"; //... (especially when spread throughout a loop)` 

is a slow operation (see here).

是一个缓慢的操作(见这里)。

small e.g.

小如

StringWriter writer = new StringWriter();
writer.write('t');
writer.write("his awesome");
String result = writer.toString();
System.out.println(result); //outputs this is awesome

A better example:

一个更好的例子:

String[] strings = /* some array of strings*/
StringWriter writer = new StringWriter();
for(String s : strings){
    writer.write(s);
}    
String result = writer.toString();
System.out.println(result);