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 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.
Just use a StreamWriter
that writes to a FileStream
:
using (StreamWriter writer = new StreamWriter("filename.txt")) {
...
}
This will of course mean that you can't change the text that is already written, like you can do in a StringBuilder
, but I assume that you are not using that anyway.
Why not streaming directly into the stream? You could use the TextWriter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With