Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 939471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:47:45+00:00 2026-05-15T21:47:45+00:00

Imagine that I have a C# application that edits text files. The technique employed

  • 0

Imagine that I have a C# application that edits text files. The technique employed for each file can be either:

1) Read the file at once in to a string, make the changes, and write the string over 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 by line, writing the changes to a temp file, then deleting the source and renaming the temp 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?

It seems to me that either reading by line or reading the entire file at once, the same quantity of data will be read, and disk times will dominate the memory alloc times. That said, once a file is in memory, the OS is free to page it back out, and when it does so the benefit of that large read has been lost. On the other hand, when working wit a temporary file, once the handles are closed I need to delete the old file and rename the temp file, which incurs a cost.

Then there are questions around caching, and prefetching, and disk buffer sizes…

I am assuming that in some cases, slurping the file is better, and in others, operating by line is better. My question is, what are the conditions for these two cases?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:47:46+00:00Added an answer on May 15, 2026 at 9:47 pm

    in some cases, slurping the file is better, and in others, operating by line is better.

    Very nearly; except that reading line-by-line is actually a much more specific case. The actual choices we want to distinguish between are ReadAll and using a buffer. ReadLine makes assumptions – the biggest one being that the file actually has lines, and they are a reasonable length! If we can’t make this assumption about the file, we want to choose a specific buffer size and read into that, regardless of whether we’ve reached the end of a line or not.

    So deciding between reading it all at once and using a buffer – always go with the easiest to implement, and most naive approach until you run into a specific situation that does not work for you – and having a concrete case, you can make an educated decision based on the information you actually have, rather than speculating about hypothetical situations.

    Simplest – read it all at once.

    Is performance becoming a problem? Does this application run against uncontrolled files, so their size is not predictable? Just a few examples where you want to chunk it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is one thing that I do not understand... Imagine you have a text
imagine that i have a web page in my application for inputing data and
Imagine that you have a simple site with only 2 pages: login.aspx and secret.aspx.
Imagine that you have several links on a page. When you clicked, a div
Is it normal to modify setter arguments? Let's imagine that we have setString method.
Imagine I have a process that starts several child processes. The parent needs to
Imagine I have the following: inFile = /adda/adas/sdas/hello.txt # that instruction give me hello.txt
Imagine this case where I have an object that I need to check a
Imagine that you want to develop a non-trivial end-user desktop (not web) application in
In my application I have several DataContexts that connects to different databases with different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.