Home > Backend Development > C++ > How Can I Efficiently Delete Lines from Large Text Files in C#?

How Can I Efficiently Delete Lines from Large Text Files in C#?

Barbara Streisand
Release: 2025-01-14 09:08:43
Original
317 people have browsed it

How Can I Efficiently Delete Lines from Large Text Files in C#?

C# efficient text file line deletion method

When processing text files, it is often necessary to delete specific lines. This article explores efficient solutions for accomplishing this task in C#, especially for processing large text files.

Solution Overview

This scheme creates a temporary file and selectively copies lines from the original file to the temporary file, excluding lines for deletion. After the target lines are processed, the temporary file replaces the original file, thereby removing the unnecessary lines.

Code implementation

<code class="language-csharp">string tempFile = Path.GetTempFileName();

using (StreamReader sr = new StreamReader("file.txt"))
using (StreamWriter sw = new StreamWriter(tempFile))
{
    string line;

    while ((line = sr.ReadLine()) != null)
    {
        if (line != "removeme")
            sw.WriteLine(line);
    }
}

File.Delete("file.txt");
File.Move(tempFile, "file.txt");</code>
Copy after login

This method ensures that only necessary lines are kept while excluding the target lines from the updated file.

Other considerations

  • Performance: For large files, this method minimizes memory usage and disk I/O by processing lines sequentially.
  • Encoding: The code assumes that the text file uses UTF-8 encoding. Adjust encoding as needed.
  • Memory handling: For smaller files, memory handling may be more efficient. Consider the following:
<code class="language-csharp">File.WriteAllLines("file.txt",
    File.ReadLines("file.txt").Where(l => l != "removeme").ToList());</code>
Copy after login

This method avoids creating temporary files, but requires immediate execution using ToList().

The above is the detailed content of How Can I Efficiently Delete Lines from Large Text Files in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template