The operation of the C # IO stream is very important, and we use this technique for reading and writing files, which shows an example of a file content copy, briefly explaining IO operations in C #.
namespace consoleapplication1{ class program { static void main (String[] args) { // Read the contents of the file into the stream stream stream = file.open ("Test.txt", filemode.openorcreate); //initialize an array of bytes byte[] bytes = new byte[(int) stream. length]; //stream is read into byte array stream. Read (bytes, 0, bytes. LENGTH); //receive &nbs with MemoryStreamp; memorystream ms = new MemoryStream (bytes); //settings from the beginning ms. Seek (0, seekorigin.begin); // Then write the returned memorystream to another file to ms. WriteTo (New filestream ("NewFile.txt", filemode.openorcreate)); } }}
Stream is an abstract class, while MemoryStream and FileStream are subclasses of Sream.
The following example demonstrates the asynchronous way to read the contents of a txt text.
namespace consoleapplication1{ class program { static void main (String[] args) { Console.WriteLine (Gettxt (). Result); } /// <summary> /// asynchronously reads txt text content /// </summary> /// <returns></returns> public static Async task<string> gettxt () { using (Stream stream = file.open (" Test.txt", filemode.openorcreate)) { using ( Streamreader sr = new streamreader (Stream, encoding.default)) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;AWAIT&NBSP;SR. Readtoendasync (); } } } }}
for more classes and operations on Io Please refer to:Https://msdn.microsoft.com/zh-cn/library/system.io (v=vs.110). aspx.
Operation of C # IO stream