C # Learning (eight)-Stream

Source: Internet
Author: User

1. Overview

The stream is used for IO processing, which has the following classes in the System.IO namespace:
Binaryreader/writer
Textreader/writer
Stream
Where class stream is an abstract class. Thus there are three derived classes:
MemoryStream: Read and Write memory
BufferedStream: Read/write buffers
FileStream: Performing read and write to files

Textreader/writer is an abstract class. Derived classes from this:
Streamreader/streamwirter
Stringreader/streamwrite

The IO operation basically requires a stream-dependent subclass. In fact, for the stream, the operation is relatively simple, as long as the details of the treatment of a little attention, I believe in the use of it can also be handy.

2. Examples of application methods

Examples of use of memory streams and buffered streams:

Static voidMain (string[] args) {MemoryStream Memstream=NewMemoryStream ();//Create a memory stream objectBufferedStream Bufstream =NewBufferedStream (Memstream);//Create a cache stream object with a memory stream object    byte[] b=New byte[Ten];  for(intI=0;i<Ten; i++) {Bufstream.writebyte (byte) (i);//writes a byte to the current location of the cache stream} bufstream.position=0; Bufstream.read (b,0,Ten);//copies bytes from the current buffered stream to an array     for(intI=0;i<Ten; i++) {Console.WriteLine ("Read result: {0}", B[i]); } Console.WriteLine ("The result reads: {0}", Bufstream.readbyte ()); //reads a byte from the underlying stream and returns the byte converted to int, or 1 if read from the end of the stream. console.readline ();} 

Output Result:

Use of file streams:

Static voidMain (string[] args) {//use of file streams:Console.WriteLine ("Please enter a path:"); strings=Console.ReadLine (); FileStream FileStream=NewFileStream (s,filemode.openorcreate); BinaryWriter Binstream=NewBinaryWriter (FileStream);  for(intI=1; i<=Ten; i++) {Binstream.write (int) (i);//writes a 4-byte signed integer to the current stream and increases the position of the stream by 4 bytes. } binstream.close ();     Filestream.close (); FileStream F=NewFileStream (S,filemode.open,fileaccess.read,fileshare.readwrite); BinaryReader buf=NewBinaryReader (f);  for(intI=1; i<=Ten; i++) {Console.WriteLine ("Output {0}", buf. ReadInt32 ());//This is possible, because int is a 32-bit    }}   

Output Result:

If you know that the file contains only text, we can use the StreamReader and StreamWriter classes, which make it easy to manipulate the text. For example, they support the ReadLine () and WriteLine () methods, which can read or output a line of text.
Application Examples:

Static voidMain () {FileInfo thesourcefile=NewFileInfo (@"E:\zzz\Program.cs"); StreamReader Reader=Thesourcefile.opentext (); StreamWriter writer=NewStreamWriter (@"E:\zzz\test1a.bak",false); //Create a text variable to the line    stringtext;  Do{//read every line writing both to the console and to the fileText =Reader.        ReadLine (); Writer.        WriteLine (text);    Console.WriteLine (text); }  while(Text! =NULL); Reader.    Close (); Writer. Close ();}

Output: (for the contents of the file E:\zzz\Program.cs)

3. Other uses

C # asynchronous file I/O

Synchronous I/O means that the method is blocked until the I/O operation is complete, and the method returns its data after the I/O operation is complete. With asynchronous I/O, users can call BeginRead. The main thread can continue to do other work, and later, the user will be able to process the data. In addition, multiple I/O requests can be suspended at the same time.

If you want to be notified when data is available, you can call EndRead or EndWrite and pass in the IAsyncResult that corresponds to the I/O request that you make. You can also provide a callback method that should call EndRead or EndWrite to calculate how many bytes have been read or written. When many I/O requests are suspended at the same time, asynchronous I/O can provide better performance, but often requires some important adjustments to your application to make it work correctly.

The stream class supports synchronous and asynchronous mixed reads and writes to the same stream, regardless of whether the operating system allows this. Stream will provide the default implementation of the asynchronous read and write operation on behalf of the synchronous implementation and provide the default implementation of synchronous read and write operations on behalf of the asynchronous implementation.

C # Learning (eight)-Stream

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.