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:
//File stream: Read FileStream FileStream = File.Open (@ "D:\test.txt", FileMode.Open); Initialize file stream byte[] array = new byte[filestream.length]; Initializes a byte array that is used to stage the read to byte filestream.read (array, 0, array. Length); // read data in stream, write to byte array filestream.close (); // Close stream string str = Encoding.Default.GetString (array); Converts the contents of a byte array into a string Response.Write (str);
Write file:
file stream: Write FileStream fileStream = File.Open (@ "D:\test.txt", filemode.append); Initialize file stream byte[] array = Encoding.Default.GetBytes (" haha 123abc"); 0, Array. Length); // writes byte array to file stream Filestream.close (); Close the 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:
Server TcpListener lis=New TcpListener (5000);//The server listens to the LIS. Start ();//Start the socket sock=lis. AcceptSocket ();//Blocking until a client connectsNetworkStream NetworkStream =New NetworkStream (sock);// Get the stream in the socket if ( netstream.dataavailable) // If the client sent a message {byte[] data = new byte [1024]; // defines a byte array to hold the received data 0, data. Length); // starting from the position, read to the end of the byte array 0, Len); // Convert the received bytes to a string}
Clients TcpClient client =New TcpClient ();//Client TCP object. Connect ("127.0.0.1", ( ); Connect Server NetworkStream mystream = client. GetStream (); // Get network stream byte[] data = Encoding.Default.GetBytes ("Hi, Hello "); // First convert the input string message to byte MyStream. Write (data, 0, data. Length); // Write Data MyStream to MyStream. Flush (); // refreshes the data mystream in the stream. 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:
"Hello hi!!" "; byte[] array = Encoding.UTF8.GetBytes (str); //new MemoryStream (array); Initialize MemoryStream class byte[] arraynew = memory. ToArray (); // convert in-memory data to byte array string strnew = Encoding.UTF8.GetString (arraynew); convert a byte array to a string
Iv. Streamreader/streamwriter
Streamreader/streamwriter is primarily used to process streaming data . They provide efficient stream read/write capabilities, respectively.
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 stream
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.
textreader/Writer reads and writes TextReader TextReader =New StringReader ("Hi, hi.");//Initialize Read stream TextWriter TextWriter =New StringWriter ();//Initializing the Write streamChar[] C=Newchar[4096int chars = 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); // read stream }string str= textwriter.tostring (); // writes data from the stream into a string textreader.close (); // close stream Textwriter.close ();
Precautions:
1. The stream must be closed after use.
2. Consider memory overflow issues when loading data into memory in the stream.
Original: https://www.cnblogs.com/zxx193/p/3580564.html
C # Flow Summary (file stream, memory stream, network stream, BufferedStream, Streamreader/streamwriter, Textreader/textwriter) "Go"