Home >Backend Development >C++ >How Can I Efficiently Read a Text File Line by Line in C# .NET?
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.
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>
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:
<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!