当前位置:网站首页>C IO stream (II) extension class_ Packer

C IO stream (II) extension class_ Packer

2022-06-26 00:43:00 Tianma 3798

One 、C# IO Stream Stream extension class

C# The following standards are included IO( Input / Output ) class , From different sources ( Such as files , Memory , The Internet , Isolated storage, etc ) To read / write in .

flow (Stream): System.IO.Stream  Is an abstract class , Provides the byte ( read , Writing, etc ) Standard method of transfer to source . Transfer bytes just like wrapper classes . Need to read from a specific source / Classes that write bytes must implement Stream class .

The following classes inherit Stream class , To provide the ability to read from a specific source / The function of writing bytes :

FileStream: Read bytes from or write bytes to a physical file , Whether it is .txt,.exe,.jpg Or any other document .FileStream Derive from Stream class .

MemoryStream: MemoryStream Read or write bytes stored in memory .

BufferedStream: BufferedStream From the other Stream Read or write bytes , To improve some I / O Performance of operation .

NetworkStream: NetworkStream Read or write bytes from a network socket .

PipeStream: PipeStream Read or write bytes from different processes .

CryptoStream: CryptoStream Used to link a data stream to a cryptographic transformation .

Two 、C# IO Stream Readers and writers

1. character string   Readers and writers

StreamReader:StreamReader Is a helper class , Used to convert bytes to characters from by using encoded values Stream Read character . It can be used from different streams ( for example FileStream,MemoryStream etc. ) Read string from ( character ).

StreamWriter:StreamWriter Is a helper class , Used to write a string to by converting characters to bytes Stream. It can be used to write strings to different streams , for example FileStream,MemoryStream etc. .

2. Binary system Readers and writers

BinaryReader:BinaryReader Is a helper class for reading raw data types from bytes .

BinaryWriter:BinaryWriter Write primitive types in binary .

The image above shows ,FileStream Read bytes from physical file , then StreamReader Read the string by converting these bytes to a string . Again ,StreamWriter Get a string and convert it to bytes , And then write FileStream, then FileStream Write bytes to a physical file . therefore ,FileStream Processing bytes ,StreamReader and StreamWriter Processing strings .

3、 ... and 、 Often tidy up

  1. Stream Is an abstract class for transferring bytes from different sources . It is the base class for all other classes , These other classes read bytes / Write to different sources .

  2. FileStream Class provides byte read and write functions for physical files .

  3. The reader / writer class provides the slave stream class (FileStream,MemoryStream etc. ) The function of reading bytes and converting bytes into appropriate encoding .

  4. StreamReader By converting bytes to strings , Provides an auxiliary method to get information from FileStream Read string from .StreamWriter Provides a helper method , You can write a string to by converting it to bytes FileStream.

more :

C# IO Stream flow ( One ) Basic concepts _ The basic definition

C# Array grouping _C# The data packet _C# Linq Use groups to organize

C# Available types in _ Type not available _C# Double question mark _C# Question mark point _C# null It's not equal to

原网站

版权声明
本文为[Tianma 3798]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206252051297591.html