Home >Backend Development >C++ >How Can I Efficiently Access a Specific Line in a Large Text File Using C#?

How Can I Efficiently Access a Specific Line in a Large Text File Using C#?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-15 09:28:44623browse

高效读取大型文本文件中的特定行 (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

  • Calculate the byte offset of the target line in the file.
  • Position the file stream to the calculated offset using the FileStream.Seek method.
  • Use StreamReader.ReadLine() to read the line.

Method 2: LINQ method (applicable to .NET 4.0 and above)

  • Use File.ReadLines(fileName).Skip(line - 1).Take(1).First().
  • This method allows direct access to individual lines without reading the entire file.

Method three: Custom line reader

  • Create a custom line reader that skips a specified number of bytes before reading a line. This method is suitable for small line numbers and can be processed efficiently.

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:

  • Use File.ReadLines(fileName).Take(25).ToArray() to read the first 25 lines.
  • Parse line 17 to determine the value of variable X.
  • Use 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!

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