Use the C#iterator to read the text file reverse
This article introduces how to use the C#iterator (Yield Return) to highly read text files to avoid loading the entire file to the memory.
Create flow source:
determine the method of creating a addressable stream or a specified file name. -
Definition code: Specify the encoding of bytes into text.
- Cushion size: The size of the buffer used to read data from flowing.
Character start detection: - Define a function to detect the starting position of the character in the code used.
Reverselinereader class:
Create a class to package the function of the above parameters. -
Implement:
- Open the stream.
Check readability and addressability.
- reverse iteration flow, detect character boundaries and decode text.
Store the end of the previous line and return in line in line.
- Treatment of special circumstances such as UTF-16 and changes.
-
- How to use:
-
Create a new ReverselineReader and call its Getenumerator () method. This method returns an Ienumerator - , which iterates iteratively.
-
<:> Note:
- This solution only supports single-byte encoding, UTF-8 and Unicode. For other long -term coding, custom processing may be required.
The above is the detailed content of How to Efficiently Read a Text File in Reverse Using a C# Iterator?. For more information, please follow other related articles on the PHP Chinese website!