C#stream Study Notes

Source: Internet
Author: User

C # temperature so know NEW: Stream article (-)

Http://www.cnblogs.com/JimmyZheng/archive/2012/03/17/2402814.html

The basic concept focuses on this article.

What is Stream?

The explanations in MSDN are too concise: Provide a general view of the sequence of bytes

(I do not want to understand this, it must make me crazy, I understand the flow is to the natural river as clear and beautiful, C # flow is the same, many technologies or core technology need to flow of help)

What is a sequence of bytes?

In fact, it is simple to understand that the byte sequence refers to:

Byte objects are stored as contiguous sequence of bytes, and bytes are sorted in a certain order to make up a sequence of bytes

What about the flow interpretation can be abstracted into the following situations:

For example: A river has a fish swim, this fish is a byte, this byte includes fish eyes, mouth, etc. composed of 8 binary, obviously this river is our core object: Flow

* 4: void Flush (): This must be said more carefully:

When we use a stream to write a file, the data stream goes into the buffer and does not write to the file immediately, and when this method is executed, the data stream of the buffer is immediately injected into the underlying stream

Description in MSDN: Use this method to move all information from the underlying buffer to its target or to clear the buffer, or to perform both operations at the same time. Depending on the state of the object, you may need to repair

Changes the current position within the stream (for example, in the case where the underlying stream supports lookups) do not flush the stream base object when using the StreamWriter or BinaryWriter class.

Instead, use the Flush or Close method of the class, which ensures that the data is first flushed to the underlying stream before it is written to the file.

(The red part is key, please be able to understand, in the future will be described in the corresponding chapters)

*9: abstract void Write (byte[] buffer,int offset,int count)

This method contains 3 key parameters: Buffer byte array, offset offset, and number of bytes read

Unlike the Read method, the first parameter in the Write method buffer already has many byte types

Data, we only need to set offset and count to write the data in the buffer to the stream

*10: virtual void Close ()

Close the stream and release the resource, and in practice, if you don't use the using, don't forget to close the stream after you've used it

This method is especially important, so don't forget to close the current stream!

C # temperature so know NEW: Stream article ( two )

Http://www.cnblogs.com/JimmyZheng/archive/2012/03/19/2405216.html#no1

The definition concepts and some considerations of TextReader and StreamReader are described in detail.

c#--stream with files (stream & file) (i)

Http://www.cnblogs.com/long-gengyun/archive/2010/03/28/1698681.html

    • Flow concept

A stream is an abstract concept of a sequence of bytes , such as the operation of a file, an input / output device, a pipeline that communicates internally , and so on, and the Stream class and its derived classes provide a general view of these different types of input and output. Such programmers do not have to be familiar with the specifics of the operating system and the underlying device, or they can operate on the stream.

three basic operations commonly used by streams:

1. Read stream: The operation is data transfer from stream to data structure

2. Write stream: Data transfer from data structure to stream during this operation

3. Stream Support Lookup: Lookup is the current position within the convection query and modification

The above three functions can be set by CanRead, Canwrite,canseek properties.

The Read and write methods support reading and writing data in various formats. For streams that support lookup, you can use the Seek and SetLength methods and the length and position properties to query and modify the current position and length of the stream.

Some streams implement local buffering of data to improve performance. For such a stream, the Flush method can be used to clear the internal buffer and ensure that all data is written to the data source or memory.

When you implement a derived class of stream, you must provide an implementation of the Read and write methods. Async methods Beginread,endread,beginwrite and EndWrite are implemented by synchronous methods read and write. ReadByte and WriteByte can implement creating a new array of cell bytes, and then call the read and write implementations.

    • File overview

A file behaves as a stream when it is manipulated, that is, a stream is a series of bytes read from some input.

File by information in the external memory on the palate encoding can be divided into text files and binary files.

The Stream class is a class in the System.IO namespace that contains all classes that allow synchronous and asynchronous reads and writes on the data stream and on the file in the System.IO namespace, following a brief introduction to the commonly used classes.

1. Directory class: Static methods that contain all operations directories, such as creating, moving, copying, deleting, etc.

2. DirectoryInfo class: Contains all the operation directory of the instance method, such as the various properties of the directory (name, creation time), directory of various operations (directory creation, existence, movement, deletion, etc.)

3. File class: Is a typical operation of the files, providing text creation, opening, copying, deleting, moving and other static methods. It can also be used to get basic information about files and settings files.

4. FileInfo class: A typical operation of the file, providing text creation, opening, copying, deleting, moving and other instance methods. When a file needs to be reused multiple times, using the instance method provided by the FileInfo class, you cannot use the static method provided by file.

5. FileStream class: This class implements the file read, write, open, close operation, support random access to the file, you can use the synchronous mode to open the file for reading and writing, you can also open the file asynchronously to read and write.

6. Path class: This class operates on a string instance that contains file or directory path information that can be performed on a cross-platform basis.

7. MemoryStream class: This class creates a stream that supports storage as memory.

8. StreamReader class: This class can read the contents of a standard text file. That is to implement a TextReader. The default encoding format is UTF-8.

9. StreamWriter class: This class can write content in a previous standard text file. That is to implement a TextWriter. The default encoding format is UTF-8.

StringReader class: This class implements a textreader that reads from a string.

StringWriter class: This class implements writing information to a string, which is stored in the underlying StringBuilder.

TextReader class: This class represents a reader that can read a continuous character system.

TextWriter class: This class represents an editor that can write an ordered series of characters, which is an abstract class.

C #: File, byte[], stream convert each other

Http://www.cnblogs.com/warioland/archive/2012/03/06/2381355.html

///conversion between C # Stream and byte[]///turn the Stream into byte[]  Public byte[] Streamtobytes (Stream stream) {byte[] bytes =New byte[Stream.    Length]; Stream. Read (Bytes,0, Bytes.    Length); //sets the position of the current stream as the start of the streamStream. Seek (0, Seekorigin.begin); returnbytes;} ///turn byte[] into a Stream  PublicStream Bytestostream (byte[] bytes) {Stream Stream=NewMemoryStream (bytes); returnstream;}
///conversion between Stream and file///write Stream to file Public voidStreamtofile (Stream stream,stringfileName) {    //convert Stream to byte[]    byte[] bytes =New byte[Stream.    Length]; Stream. Read (Bytes,0, Bytes.    Length); //sets the position of the current stream as the start of the streamStream. Seek (0, Seekorigin.begin); //write byte[] to fileFileStream fs =NewFileStream (FileName, FileMode.Create); BinaryWriter BW=NewBinaryWriter (FS); Bw.    Write (bytes); Bw.    Close (); Fs. Close ();}
///read Stream from File  PublicStream Filetostream (stringfileName) {                //Open FileFileStream FileStream =NewFileStream (FileName, FileMode.Open, FileAccess.Read, FileShare.Read); //read the file byte[]    byte[] bytes =New byte[Filestream.length]; FileStream.Read (Bytes,0, Bytes.    Length);    Filestream.close (); //convert byte[] to StreamStream stream =NewMemoryStream (bytes); returnstream;}

FileStream read-write file "StreamWriter and StreamReader"

Http://www.cnblogs.com/yank/archive/2007/11/16/961878.html

C#stream Study Notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.