当前位置:网站首页>C IO stream (I) basic concept_ Basic definition
C IO stream (I) basic concept_ Basic definition
2022-06-26 00:42:00 【Tianma 3798】
One 、C# Stream Flow basic concept
Concept : Provides a general view of byte sequences , Is an abstract class .
Data flow (Stream) Is an abstract representation of serial transmission , It's about input / An abstraction of output . Data has source and destination , Connecting the two is the streaming object .

The flow has the following 3 Basic operations :
write in : Transfer data from memory buffer to external source ;
Read : Transfer data from an external source to a memory buffer ;
lookup : Reset the current position of the stream , For random reading and writing . But not all stream types support lookup , For example, the network flow class does not have the concept of current location , Search is not supported .
Two 、C# Stream Basic definition of flow , Common properties and methods
Stream It's a virtual class , It and its derived classes provide Read and Write Method , It can support reading and writing data at the byte level .Read Method reads bytes from the current byte stream and puts them into the memory buffer ,Write Method writes the bytes of the memory buffer to the current stream .
attribute :
1:CanRead: Read-only property , Determine whether the stream can be read
2:CanSeek: Read-only property , Determine whether the stream supports trace lookup
3:CanSeek: Read-only property , Determine whether the stream is writable
4.Length: Indicates the length of the stream
5.Position attribute
Although you can see it literally Position Property is just a place in the flow , But in the actual development, we will find that this idea will be very naive ,
quite a lot asp.net Many friends will experience such a pain when uploading files or pictures in the project :Stream Object is cached , Led to Position Property cannot be in stream
Find the right place , It's crazy , In fact, it's easy to solve this problem , You must have thought of it , In fact, every time we use streams, we must Stream.Position
Set to 0 That's it , But that doesn't solve the problem , The best way is to use Using Statement wraps the flow object , Close recycling after use .
Method :
1.void Flush()
When we use streaming to write files , The data stream enters the buffer first , Instead of writing to a file immediately , When this method is executed , The buffer's data stream is immediately injected into the underlying stream
MSDN Description in : Use this method to move all information from the underlying buffer to its destination or clear the buffer , Or both . Based on the state of the object , It may need to be repaired
Current position in diverter ( for example , This is the case when the underlying flow supports lookups ) When using StreamWriter or BinaryWriter Class time , Do not refresh Stream Base object .
Instead, the Flush or Close Method , This method ensures that the data is first refreshed to the underlying flow , Then write it to a file .
2: virtual void Close()
Close flow and release resources , In practice , If not using Words , Don't forget to close the stream after using it
This method is particularly important , Do not forget to close the current stream after using it !
3:abstract int Read(byte[] buffer ,int offset ,int count)
This method contains 3 Key parameters : Buffer byte array , Displacement offset and number of bytes read , The total number of bytes in a buffer is returned each time a byte is read
The first parameter : This array is equivalent to an empty box ,Read() Method reads a byte in the stream and places it in the empty box .( It can be used after reading all buffer Byte array )
The second parameter : Represents the displacement deviation , Tell us where to get from the stream ( Offset ) Start reading .
Last parameter : Is how many bytes to read .
The return value is the total number of bytes read .
4.abstract void Write(byte[] buffer,int offset,int count)
This method contains 3 Key parameters : Buffer byte array , Displacement offset and number of bytes read
and read The difference is write First parameter in method buffer There are already many byte type
The data of , We just need to set up offset and count to buffer Data in write stream
5. abstract long Seek(long offset,Seek)
You remember Position Attribute ? Actually Seek The method is to reset a position in the flow , In explanation offset Before parameter function, let's understand SeekOrigin This enumeration :
If offset Negative , New location is required origin Before the specified location , The interval difference is offset Number of bytes specified . If offset zero (0), The new location is required to be located by the origin At the designated location .
If offset Being positive , New location is required origin After the specified location , The interval difference is offset Number of bytes specified .
Stream. Seek(-3,Origin.End); Indicates that the flow end is the first 3 A place
Stream. Seek(0,Origin.Begin); Indicates at the beginning of the flow
Stream. Seek(3,Orig`in.Current); Indicates the third position after the current position of the flow
After the lookup, a new location in a stream is returned . In fact, you can understand that Seek The subtlety of the method .
more :
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
C# data type 、 Variable 、 Scope
边栏推荐
- 86. (cesium chapter) cesium overlay surface receiving shadow effect (gltf model)
- anchor free dection简介
- Openresty chapter 01 introduction and installation configuration
- 实现异步的方法
- Maintenance and key points of SMT Mounter
- Tensorrt PB to UF problem
- 【OEM专场活动】清“芯”夏日,有奖征文
- 事物/现象/事情/东西/情况/表象
- Learn to identify follow-up questions in dialogue Q & A
- Compiler Telegram Desktop end (tdesktop) en utilisant vs2022
猜你喜欢

Is camkiia the same as gcamp6f?

JS to input the start time and end time, output the number of seasons, and print the corresponding month and year

Flink reports error: a JNI error has occurred, please check your installation and try again

Compiler Telegram Desktop end (tdesktop) en utilisant vs2022

How to deliver a shelter hospital within 48 hours?

When installing PSU /usr/bin/ld:warning: -z lazload ignore
![Mysql5.7 is in the configuration file my Ini[mysqld] cannot be started after adding skip grant tables](/img/b2/2b87b3cea1422e2a860f5e0e7dcc40.png)
Mysql5.7 is in the configuration file my Ini[mysqld] cannot be started after adding skip grant tables

1-11Vmware虚拟机常见的问题解决

7. common instructions (Part 2): common operations of v-on, v-bind and V-model

How to design the product roadmap?
随机推荐
Learn to identify follow-up questions in dialogue Q & A
Comprehensive introduction to Simulink solver
Redux workflow + complete code of small examples
The development context of Ba Kong Yuan universe industry
Resolve thread concurrency security issues
SQL中只要用到聚合函数就一定要用到group by 吗?
Redux workflow explanation + small examples
鼠标拖拽围绕某个物体旋转展示
Apache基金会正式宣布Apache InLong成为顶级项目
Ffmpeg version switching
Multi-Instance Redo Apply
Servlet response download file
1-9Vmware中网络配置
Megacli common command collation
10.2.2、Kylin_ Kylin installation, uploading and decompressing, verifying environment variables, starting and accessing
leetcode. 14 --- longest public prefix
Shenzhen Taipower: the way of "communication" of the United Nations
What are AOI, X-ray and ICT in SMT industry? What does it do?
JS to input the start time and end time, output the number of seasons, and print the corresponding month and year
元宇宙中的法律与自我监管