Detailed explanation of C# stream (2) - the relationship between FileStream, BinaryReader, MemorySream, SreamReader, etc.

【File stream】

There are many files on the computer, such as text files, audio files, video files, picture files, etc. These files will be persistently stored on the disk, and their essence is a bunch of binary data.

FileStream is used to read binary files. All files on your computer, whether they are text, audio, video or other files in any format, can be read with FileStream. However, what is read is binary data, and you need to know how to deserialize it to get the corresponding understandable information. For example, the png format of pictures and the mp4 format of videos are all public deserialization methods.

Through the file stream (FileStream), it is possible to read files at a finer granularity, part by part, or even byte by byte, instead of reading files one by one.

From the perspective of flow as a whole, things are constantly moving from one place to another, emphasizing this process. For example, the flow of people entering a high-speed rail station is from outside the station to inside the station, and the flow of files is from disk to memory. Specifically, it is the basic unit in the flow From one place to another, for example, the basic unit of human flow is a person, and the basic unit of file flow is a byte.

If there are a lot of people, five people enter the station in groups. This is also a flow, and the basic unit is five people. If the file is large and divided into many files, reading each file is also a stream, and the basic unit is a file.

TextReader/TextWriter is used to efficiently process the reading and writing of text files. The basic unit is a character, but they are abstract classes and cannot be instantiated. StreamReader/StreamWriter is used to convert between text and binary. (Json files, cpp files, cs files, prefab files, etc. are all text files)

BinaryReader/BinaryWriter is used to read and write binary files, and the basic unit is bytes.

other streams

FileStream is for reading from disk to memory and writing from memory to disk.

MemoryStream is used to read and write in memory and is used to solve the exchange of various streams in memory.

The NetWorkStream network stream is used to process the communication flow between the server and the client.

BufferedStream :  BufferedStream reads or writes bytes from other Streams to improve the performance of certain I/O operations.

GZipStream: stream used to implement compression and decompression

Cryptostream: stream for encryption and decryption

Basic operations of file streams FileMode

  • Open: Open the file, provided that the file already exists
  • Create: Create a file
  • OpenOrCreate: Open the file, if the file does not exist, create the file
  • Append: append content, that is, add new content to the file
  • Truncate: Clear the contents of the file when opening the file, simplifying the steps of deleting the file and creating a file with the same name.

File read and write permissionsFileAccess

  • Read: read permission
  • Write: write permission
  • ReadWrite: read and write permissions

【Stream class】

Stream is an abstract class of streams. Any stream needs to inherit this class. There are various stream implementations predefined by C#. These implementations let us not care about information such as the interaction between the operating system and basic devices.

The three basic operations of streams are reading, writing, and searching, which correspond to the three basic properties CanRead, CanWrite, and CanSeek respectively.

common attributes

  • Length: The length of the stream, that is, the size of the file. The file itself will record information about the size, which can be obtained only by reading the file header, and does not need to read the entire file to know the size. It should be noted that due to memory alignment and other reasons, the file size is not necessarily equal to the memory footprint.
  • Position: The position of the current read-write stream. Treat the file content as a long byte array. Position is equivalent to Index. At the beginning of the file, Position=0; at the end of the file, Position= Array.Length-1

Common methods

  • abstract long Seek(long offset, SeekOrigin origin): Specifies the position of the read and write stream. Stream.Seek(-10,Origin.End); indicates the 10th position from the end of the stream; Stream.Seek(0,Origin.Begin); indicates the beginning position of the stream; Stream.Seek(1,Orig` in.Current); means the first position from the current position of the stream.
  • abstract int Read (byte[] buffer, int offset, int count); read data from the current position of the stream and put it into a buffer, the length of the read data is count, and the starting position in the buffer is offset, and return the actual The length read from the stream. Generally speaking, the return value is equal to count. If it is at the end of the stream, it may be read directly, and the return value is smaller than count.
  • abstract void Write (byte[] buffer, int offset, int count): Write data from the position of the current stream, the data comes from the buffer, and start writing from the offset position of the buffer, and the length of the written data is count.
  • Close() and Dispose(): There is nothing to say, but after reading the data and performing a series of processing, you may have forgotten to call them. It is recommended to write the code in a certain order. After reading the file, write close and dispose immediately, and then handle the intermediate data processing process.

Guess you like

Origin blog.csdn.net/enternalstar/article/details/132354443