Home >Backend Development >C++ >How Can I Efficiently Read a Text File Line by Line in C# .NET?

How Can I Efficiently Read a Text File Line by Line in C# .NET?

Barbara Streisand
Barbara StreisandOriginal
2025-01-29 08:30:131077browse

How Can I Efficiently Read a Text File Line by Line in C# .NET?

Read text files efficiently: processing

Question:

How to read text files in high efficiency in C# .NET?

Answer:

According to specific needs, there are many ways to optimize file reading. Use streamReader.readline:

Compared with 128 bytes of the buffer, using StreamReader and setting an appropriate buffer size (e.g., the default 1024 or larger) can significantly improve performance.

Use File.readlines:

This is a replacement method for optimization for row processing, which avoids the memory that takes up all rows at the same time.
<code class="language-csharp">using (var fileStream = File.OpenRead(fileName))
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, 1024)) {
    string line;
    while ((line = streamReader.ReadLine()) != null)
    {
        // 处理每一行
    }
}</code>

Use File.readalllines:

Although a string [] is returned, this method is more demand for memory than the above method. However, it allows random access.

<code class="language-csharp">var lines = File.ReadLines(fileName);
foreach (var line in lines)
    // 处理每一行</code>

Use string.split:

This method is usually slow and densely memory. It divides the entire content of the file into an array containing each row.

<code class="language-csharp">var lines = File.ReadAllLines(fileName);
for (var i = 0; i < lines.Length; i++)
    // 处理每一行</code>
The benchmark test is essential for determining the best method according to your specific file size and processing requirements. For high -efficiency processing, it is generally recommended to use File.Readlines.

The above is the detailed content of How Can I Efficiently Read a Text File Line by Line in C# .NET?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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