Home >Backend Development >C++ >How Can I Efficiently Access a Specific Line in a Large Text File Using C#?
C# efficiently reads specific lines in large text files
Need to read specific lines from a large text file, avoiding the memory-intensive solution of storing the entire file as an array of strings. The following methods can be achieved:
Method 1: Byte Offset Method
FileStream.Seek
method. StreamReader.ReadLine()
to read the line. Method 2: LINQ method (applicable to .NET 4.0 and above)
File.ReadLines(fileName).Skip(line - 1).Take(1).First()
. Method three: Custom line reader
Example (Method 1)
<code class="language-csharp">public static string GetLine(string fileName, int line) { using (var sr = new StreamReader(fileName)) { // ... (代码略,此处需补充计算字节偏移量和读取行的逻辑) ... } }</code>
Processing structured documents
For structured documents, you can use the following strategies:
File.ReadLines(fileName).Take(25).ToArray()
to read the first 25 lines. X
. File.ReadLines(fileName).Skip(25).Take(X).ToArray()
to read the next X
lines. These methods allow you to efficiently read specific lines from a large text file without loading the entire file into memory. Which method you choose depends on the file's specific size, structure, and .NET Framework version.
The above is the detailed content of How Can I Efficiently Access a Specific Line in a Large Text File Using C#?. For more information, please follow other related articles on the PHP Chinese website!