Imagine I have a C # application that edits text files. The technique used for each file can be:
1) Read the file immediately on the line, make changes and write the line on top of the existing file:
string fileContents = File.ReadAllText(fileName);
using (StreamWriter writer = new StreamWriter(fileName))
{
writer.Write(fileContents);
}
2) Read the file line by line, write the changes to the temp file, then delete the source and rename the temporary file:
using (StreamReader reader = new StreamReader(fileName))
{
string line;
using (StreamWriter writer = new StreamWriter(fileName + ".tmp"))
{
while (!reader.EndOfStream)
{
line = reader.ReadLine();
writer.WriteLine(line);
}
}
}
File.Delete(fileName);
File.Move(fileName + ".tmp", fileName);
What are the performance considerations with these options?
, , , , . , , , , , . , , , , .
, ...
, , - . , ?