Stream StringBuilder to file

GreyCloud picture GreyCloud · Nov 4, 2011 · Viewed 31.5k times · Source

I need to create a large text document. I currently use StringBuilder to make the document and then call File.WriteallText(filename,sb.ToString). Unfortunately, this is now starting to throw out of memory exceptions.

Is there a better way to stream a StringBuilder to file or is there some other technique I should be using?

Answer

Marc Gravell picture Marc Gravell · Nov 4, 2011

Instead of using StringBuilder, try using TextWriter (which has a broadly similar API, but which can write to a number of underlying destinations, including files) - i.e.

using(TextWriter writer = File.CreateText(path))
{
    // loop etc
    writer.Write(...);
}

More generally, it is worth separating the code that knows about files from the code that knows about how to write the data, i.e.

using(var writer = File.CreateText(path))
{
    Serialize(writer);
}
...
void Serialize(TextWriter writer)
{
    ...
}

this makes it easier to write to different targets. For example, you can now do in-memory too:

var sw = new StringWriter();
Serialize(sw);
string text = sw.ToString();

The point being: your Serialize code didn't need to change to accomodate a different target. This could also be writing directly to a network, or writing through a compression/encryption stream. Very versatile.