Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read, replace and write very large files? [duplicate]

Tags:

Possible Duplicate:
How to read a large (1 GB) txt file in .NET?

What is the optimal way in C# to read an file, replace some strings and write in another new file? I need to do this with very large files like 8GB or 25GB.

like image 218
iLevi Avatar asked Apr 13 '12 18:04

iLevi


People also ask

What text editor can open large files?

Large file text editorUltraEdit has no real limit on file size - and can easily open, edit, and save large text files in excess of 4 GB! If playback doesn't begin shortly, try restarting your device. Your browser can't play this video.

Which of the following commands covered in lecture would you use to quickly find and replace specific text in a very large dataset?

sed - Replace text quickly in very large file - Unix & Linux Stack Exchange.


1 Answers

There isn't much you can optimize about the I/O, most of the optimization should be on the string comparison to determine if the string should be replaced or not, basically you should do this

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;
}

What is your criteria to find and replace a string?

like image 178
oamilkar Avatar answered Sep 20 '22 02:09

oamilkar