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.
using (var fileStream = File.OpenRead(fileName)) using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, 1024)) { string line; while ((line = streamReader.ReadLine()) != null) { // 处理每一行 } }
Although a string [] is returned, this method is more demand for memory than the above method. However, it allows random access.
var lines = File.ReadLines(fileName); foreach (var line in lines) // 处理每一行
Use string.split:
var lines = File.ReadAllLines(fileName); for (var i = 0; i < lines.Length; i++) // 处理每一行
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!