English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C# includes the following standard IO (input/Output) class, which can be read from different sources (such as files, memory, network, isolated storage, etc.)./Write.
Stream (Stream): System.IO.Stream It is an abstract class that provides standard methods for transmitting bytes (read, write, etc.) to the source. It transmits bytes like a wrapper class. It needs to be read from a specific source./Classes that must implement the Stream class to write bytes.
The following classes inherit the Stream class to provide reading/Functionality of writing bytes:
FileStream:Reads bytes from a physical file or writes bytes to a physical file, regardless of whether it is a .txt, .exe, .jpg, or any other file. FileStream is derived from the Stream class.
MemoryStream: MemoryStream reads or writes bytes stored in memory.
BufferedStream: BufferedStream reads or writes bytes from other Streams to improve certain I / O operation performance.
NetworkStream: NetworkStream reads or writes bytes from or to a network socket.
PipeStream: PipeStream reads or writes bytes from different processes.
CryptoStream: CryptoStream is used to link data streams to cryptographic transformations.
The following diagram shows the hierarchy of stream classes:
StreamReader: StreamReader is a helper class used to read characters from Stream by converting bytes into characters using encoding values. It can be used to read strings (characters) from different streams (such as FileStream, MemoryStream, etc.).
StreamWriter: StreamWriter is a helper class that writes strings to Stream by converting characters into bytes. It can be used to write strings to different streams, such as FileStream, MemoryStream, etc.
BinaryReader: BinaryReader is a helper class used to read primitive data types from bytes. It is used to read strings from different streams (such as FileStream, MemoryStream, etc.) by using encoding values.
BinaryWriter: BinaryWriter is used to write primitive types in binary.
The above image shows that FileStream reads bytes from a physical file, and then StreamReader reads strings by converting these bytes into strings. Similarly, StreamWriter gets a string and converts it into bytes, then writes it to FileStream, and then FileStream writes the bytes to a physical file. Therefore, FileStream handles bytes, while StreamReader and StreamWriter handle strings.
Stream is an abstract class used to transmit bytes from different sources. It is the base class for all other classes, which read bytes from various sources./Write to different sources.
The FileStream class provides byte read/write functionality to physical files.
The reader class provides functionality to read bytes from stream classes (FileStream, MemoryStream, etc.) and convert bytes to the appropriate encoding.
StreamReader provides an auxiliary method to read strings from FileStream by converting bytes to strings. StreamWriter provides a helper method that can write strings to FileStream by converting strings to bytes.
Learn how to read in the next section/Write to the file system.