StreamReader to file?

I have an input stream wrapped in System.IO.StreamReader ... I want to write the content of the stream to a file (i.e. StreamWriter).

The length of the input stream is unknown. It can be several bytes and up to gigabytes in length.

What's the easiest way to do this that doesn't take up too much memory?

+2


source to share


1 answer


Something like that:

public static void CopyText(TextReader input, TextWriter output)
{
    char[] buffer = new char[8192];
    int length;
    while ((length = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, length);
    }
}

      



Note that this is very similar to what you are writing to copy the contents of one stream to another - it's just text data instead of binary data.

+10


source







All Articles