C # Flow Summary (file stream, memory stream, network stream, BufferedStream, Streamreader/streamwriter, Textreader/textwriter)

Source: Internet
Author: User
Tags array to string

One, file stream

The FileStream class is primarily used to read and write disk files. Often used to store data or read configuration files to disk.

Read file:
Copy Code

//file stream: ReadFileStream FileStream = File.Open (@"D:\test.txt", FileMode.Open);//initializing a file streambyte[] Array =New byte[Filestream.length];//Initializes a byte array that is used to stage the bytes read toFileStream.Read (Array,0, array. Length);//reads data from the stream, writes to the byte arrayFilestream.close ();//Close the streamstringstr = Encoding.Default.GetString (array);//Converts the contents of a byte array into a stringResponse.Write (str);


Write file:

 //  file stream: Write  FileStream FileStream = File.Open (@ "  ", filemode.append); //  Initialize the file stream  byte  [] array = Encoding.Default.GetBytes (  haha 123abc  ); //  filestream.write (array, 0 , Array. Length); //  filestream.close (); //  close stream  



Second, the network flow

The NetworkStream class is a stream specifically designed to handle server-to-client communication. It is often used in network programming, primarily to handle streams such as sockets, TcpClient, and TcpListener.

Single TCP synchronization mode, the server communicates with the client:
Copy Code

Server

TcpListener lis=NewTcpListener ( the);//Server MonitoringLis. Start ();//StartSocket Sock=lis. AcceptSocket ();//blocking until a client connectsNetworkStream NetworkStream=NewNetworkStream (sock);//get the stream in the socketif(netstream.dataavailable)//If the client sends a message{   byte[] data =New byte[1024x768];//defines a byte array to hold the received data   intLen = netstream.read (data,0, data. Length);//start from position, read to end of byte array   stringline = Encoding.Default.GetString (data,0, Len);//converts the received bytes to a string}


Client

TcpClient client =NewTcpClient ();//Client TCP ObjectClient. Connect ("127.0.0.1", the);//connecting to a serverNetworkStream mystream = client. GetStream ();//get the network stream                byte[] data = Encoding.Default.GetBytes ("Hi , hi.");//First, convert the input string message to bytesMyStream. Write (data,0, data. Length);//Write data to MyStreamMyStream. Flush ();//Refresh data in a streamMyStream. Close ();


Three, memory flow

The MemoryStream class is primarily used to manipulate data in memory. For example, the transmission of data in the network can be in the form of streams, when we receive these streaming data can be declared MemoryStream class to store and process them.

MemoryStream Action string:

 string  str =  " hi! Hello!   ;  byte  [] array = Encoding.UTF8.GetBytes (str); //  MemoryStream memory = new  MemoryStream (array); //  initialize MemoryStream class  byte  [] arraynew = memory. ToArray (); //  string  strnew = Encoding.UTF8.GetString (arraynew); //  



Iv. Streamreader/streamwriter

Streamreader/streamwriter is primarily used to process streaming data. They provide efficient stream read/write capabilities, respectively.

Read:

// StreamReader Read New StreamReader (@ "D:\test.txt", Encoding.default); // Initialize read set encoding format, otherwise Chinese will be garbled string readstr = reader. ReadLine (); // reads a line of reader from the stream  . READTOEND () reads all reader. Close (); // Close the stream


Write:

// StreamWriter Write New StreamWriter (@ "D:\test1.txt"); // initialization is written to writer. WriteLine ("hi Hello 123"); // writes a line of writer. Close ();



Wu, Textreader/textwriter

The Textreader/textwriter class is primarily used to process streaming data. They provide efficient text stream read/write capabilities, respectively.
Copy Code

//textreader/writer Reading and writingTextReader TextReader =NewStringReader (" Hi, hi.");//Initialize read streamTextWriter TextWriter =NewStringWriter ();//initializing the Write streamChar[] c=New Char[4096];intchars =0; while(chars = Textreader.read (c,0,4096)) >0)//writes data from the stream into a character array to read the data in the stream{textwriter.write (c,0,4096);//reading a stream from a character array}stringStr= textwriter.tostring ();//writes data in a stream to a stringTextreader.close ();//Close the streamTextwriter.close ();

Precautions:

1. The stream must be closed after use.

2. Consider memory overflow issues when loading data into memory in the stream.

C # Flow Summary (file stream, memory stream, network stream, BufferedStream, Streamreader/streamwriter, Textreader/textwriter)

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.