Requirements: I wrote before that the file format is checked by the header of the file, actuallyYou only need to read the first few bytes of the file to judge, and you don't need to read the entire fileThis article uses Stream to read the first few bytes of a disk file and optimize memory space, which is actually what it isRead on demand。
Review:
useFile.ReadAllBytesReading the entire application file, you can see that the program is very occupied. As shown below:
In fact, it only takes 2 bytes to read to determine whether the file is an application or notStreamReaderRead the first 2 bytes of the file as shown below:
The hyperlink login is visible.
You can see from the original memoryReduced usage from 287.11 MB to 20.55 MB(Without reading the file, the default memory is taken up after the program starts: 20.02 MB), which shows that a lot of unnecessary memory expenses are saved. The code is as follows:
If we only want to see the beginning of the text, we can use the more command to view it, how to use .NET/C# code to read the text content?
It is read in two ways, and the code is as follows:
Since this file is plain text content,If it is UTF-8 or other encoding format, the first few bytes of the file need to be ignored, that is, the position of the stream needs to be set (e.g., Position = 2)。
(End)
|