3

私はTextReaderオブジェクトを持っています。

TextReaderここで、コンテンツ全体をファイルにストリーミングしたいと思います。ReadToEnd()コンテンツのサイズが大きくなる可能性があるため、一度にすべてを使用してファイルに書き込むことはできません。

ブロックでこれを行う方法のサンプル/ヒントを教えてもらえますか?

4

3 に答える 3

5
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);
}
于 2014-07-12T16:42:37.607 に答える
1

このようなもの。リーダーが返されるまでループしてnull、作業を行います。完了したら、閉じます。

String line;

try 
{
  line = txtrdr.ReadLine();       //call ReadLine on reader to read each line
  while (line != null)            //loop through the reader and do the write
  {
   Console.WriteLine(line);
   line = txtrdr.ReadLine();
  }
}

catch(Exception e)
{
  // Do whatever needed
}


finally 
{
  if(txtrdr != null)
   txtrdr.Close();    //close once done
}
于 2014-07-12T16:43:05.083 に答える
0

使用TextReader.ReadLine:

// assuming stream is your TextReader
using (stream)
using (StreamWriter sw = File.CreateText(@"FileLocation"))
{
   while (!stream.EndOfStream)
   {
        var line = stream.ReadLine();
        sw.WriteLine(line);
    }
}
于 2014-07-12T16:45:29.683 に答える