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>
This method ensures that only necessary lines are kept while excluding the target lines from the updated file.
Other considerations
<code class="language-csharp">File.WriteAllLines("file.txt", File.ReadLines("file.txt").Where(l => l != "removeme").ToList());</code>
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!