C#でファイルを読み取り、文字列を置き換えて別の新しいファイルに書き込む最適な方法は何ですか? 8GB や 25GB などの非常に大きなファイルでこれを行う必要があります。
10836 次
3 に答える
11
I/O について最適化できることはあまりありません。ほとんどの最適化は、文字列を置換する必要があるかどうかを判断する文字列の比較で行う必要があります。基本的には、これを行う必要があります。
protected void ReplaceFile(string FilePath, string NewFilePath)
{
using (StreamReader vReader = new StreamReader(FilePath))
{
using (StreamWriter vWriter = new StreamWriter(NewFilePath))
{
int vLineNumber = 0;
while (!vReader.EndOfStream)
{
string vLine = vReader.ReadLine();
vWriter.WriteLine(ReplaceLine(vLine, vLineNumber++));
}
}
}
}
protected string ReplaceLine(string Line, int LineNumber )
{
//Do your string replacement and
//return either the original string or the modified one
return Line;
}
文字列を検索して置換する基準は何ですか?
于 2012-04-13T20:12:45.583 に答える
2
于 2012-04-13T19:29:03.273 に答える
1
大きすぎない線はありますか?その場合、ファイルを 1 行ずつ読み込み、その行で置換を行い、その行を新しいファイルに書き出すことができます。ストリーミングされるため、メモリはほとんど必要ありません。
于 2012-04-13T18:51:37.940 に答える