I have huge text file say of 8 GB, need to replace some specific lines in it. I have found the lines and also replaced them. But the problem is I am rewriting each line to file. Just want to replace a line without rewriting complete file again. The text file contains Base64 string written line by line.I am reading the file line by line and whenever match found want to replace only the current line (Without creating another text file or rewriting all text in file) .Any help would be greatly appreciated. Using the below code. Each line is being read and written again however I am replacing only one file. See code below.
string originalFile = "Original.txt";
string tempFile = "temp.txt";
string binayString = "base64string";
using (StreamWriter file2 = new StreamWriter(System.IO.File.Open(tempFile, FileMode.Create, FileAccess.Write)))
{
StreamReader sr = new StreamReader(originalFile);
String line = string.Empty;
while ((line = sr.ReadLine()) != null)
{
if (line.StartsWith("Somestartpoint")
{
file2.WriteLine("Somestartpoint" + binayString);
continue;
}
file2.WriteLine(line);
}
sr.Dispose();
}
System.IO.File.Replace(tempFile, originalFile, "backup.txt");
System.IO.File.Delete(checkPath + "backup.txt");