Search code examples
c#streamtextreader

Stream TextReader to a File


I have a TextReader object.

Now, I want to stream the whole content of the TextReader to a File. I cannot use ReadToEnd() and write all to a file at once, because the content can be of high size.

Can someone give me a sample/tip how to do this in Blocks?


Solution

  • using (var textReader = File.OpenText("input.txt"))
    using (var writer = File.CreateText("output.txt"))
    {
        do
        {
            string line = textReader.ReadLine();
            writer.WriteLine(line);
        } while (!textReader.EndOfStream);
    }