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?
Instead of using
StringBuilder, try usingTextWriter(which has a broadly similar API, but which can write to a number of underlying destinations, including files) – i.e.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.
this makes it easier to write to different targets. For example, you can now do in-memory too:
The point being: your
Serializecode 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.