私はTextReader
オブジェクトを持っています。
TextReader
ここで、コンテンツ全体をファイルにストリーミングしたいと思います。ReadToEnd()
コンテンツのサイズが大きくなる可能性があるため、一度にすべてを使用してファイルに書き込むことはできません。
ブロックでこれを行う方法のサンプル/ヒントを教えてもらえますか?
私はTextReader
オブジェクトを持っています。
TextReader
ここで、コンテンツ全体をファイルにストリーミングしたいと思います。ReadToEnd()
コンテンツのサイズが大きくなる可能性があるため、一度にすべてを使用してファイルに書き込むことはできません。
ブロックでこれを行う方法のサンプル/ヒントを教えてもらえますか?
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);
}
このようなもの。リーダーが返されるまでループして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
}
// assuming stream is your TextReader
using (stream)
using (StreamWriter sw = File.CreateText(@"FileLocation"))
{
while (!stream.EndOfStream)
{
var line = stream.ReadLine();
sw.WriteLine(line);
}
}