我什么时候应该使用 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
When should I use Java's StringWriter?
提问by MarkusWillson
回答by Jaskey
It is a specialized Writer
that writes characters to a StringBuffer
, and then we use method like toString()
to get the string result.
它是一种专门的Writer
将字符写入 a StringBuffer
,然后我们使用 like 方法toString()
获取字符串结果的方法。
When StringWriter
is used is that you want to write to a string, but the API is expecting a Writer
or a Stream
. It is a compromised, you use StringWriter
only when you have to, since StringBuffer
/StringBuilder
to 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 InputStream
to chars on a Writer
so that we can get String
later, 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-char
or string-by-string
.
它用于构造字符串char-by-char
或string-by-string
.
It is similar to StringBuilder
but uses a StringBuffer
under 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
).
它类似于StringBuilder
但StringBuffer
在引擎盖下使用了一个。当您使用需要流或编写器的 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);