I know, that i can use ReadAllLines method and edit line using it's index. But this way not good for huge files. For example, i have file with 800,000 lines, and i need to find a line, which contains "hello", and insert "\tmy\tfriend" string after this word. I can't read all file into RAM. Which way is the best for HUGE files?
I tried to use StreamWriter, but it's append the end of the file.
var writer = new StreamWriter(File.Open(...));
Unfortunately you have to rewrite the entire file every time.
Read the first file and write back to a second temporary file. When you encounter the line that matches your search append the the additional string and continue write the rest of the file. At the end replace the original file and delete the temporary one.
Instead of
File.Copy+File.Deleteyou could also useFile.Move.