如何读取,替换和写入非常大的文件?

iLe*_*evi 6 c#

可能重复:
如何在.NET中读取大型(1 GB)txt文件?

C#读取文件,替换某些字符串并写入另一个新文件的最佳方式是什么?我需要使用非常大的文件,如8GB或25GB.

小智 8

关于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;
}
Run Code Online (Sandbox Code Playgroud)

查找和替换字符串的标准是什么?