将 StringBuilder 写入流 [英] Write StringBuilder to Stream
本文介绍了将 StringBuilder 写入流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将 StringBuilder 写入 System.IO.Stream 的最佳方法是什么?
What is the best method of writing a StringBuilder to a System.IO.Stream?
我目前正在做的:
StringBuilder message = new StringBuilder("All your base");
message.Append(" are belong to us");
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Text.ASCIIEncoding encoding = new ASCIIEncoding();
stream.Write(encoder.GetBytes(message.ToString()), 0, message.Length);
推荐答案
不要使用 StringBuilder,如果您要写入流,请使用 StreamWriter:
Don't use a StringBuilder, if you're writing to a stream, do just that with a StreamWriter:
using (var memoryStream = new MemoryStream())
using (var writer = new StreamWriter(memoryStream ))
{
// Various for loops etc as necessary that will ultimately do this:
writer.Write(...);
}
这篇关于将 StringBuilder 写入流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文