When should I rip a file, and when should I read it line by line?

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

// make changes to fileContents here...

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();
            // make changes to line here
            writer.WriteLine(line);
        }
    }
}
File.Delete(fileName);
File.Move(fileName + ".tmp", fileName);

What are the performance considerations with these options?

, , , , . , , , , , . , , , , .

, ...

, , - . , ?

+3
1

, - .

; , . , , - ReadAll . ReadLine - , , ! , , , .

, - , , , - , , , , .

- .

? , ? , .

+4

Source: https://habr.com/ru/post/1755645/


All Articles