C # Byte [] array reading and writing files,
Protected void ByteToString_Click (object sender, EventArgs e) {string content = this.txt Content. text. toString (); if (string. isNullOrEmpty (content) {return;} // convert string to byte array byte [] array = Encoding. UTF8.GetBytes (content); // convert the byte array to string result = Encoding. UTF8.GetString (array); Response. write (result);} // use byte [] array to Write the file protected void writerFile_Click (object sender, EventArgs e) {string content = this.txt Content. text. toString (); if (string. isNullOrEmpty (content) {return;} // convert string to byte array byte [] array = Encoding. UTF8.GetBytes (content); string path = Server. mapPath ("/test.txt"); // create a file stream FileStream fs = new FileStream (path, FileMode. create); // write the byte array to the file fs. write (array, 0, array. length); // close the stream for all stream types; otherwise, memory leakage may occur. close (); Response. write ("saved file succeeded");} // read the file protected void readFile_Click (object sender, EventArgs e) using byte [] Array {string path = Server. mapPath ("/test.txt"); FileStream fs = new FileStream (path, FileMode. open); // get the file size long size = fs. length; byte [] array = new byte [size]; // read the file to the fs in the byte array. read (array, 0, array. length); fs. close (); // convert the byte array to string result = Encoding. UTF8.GetString (array); Response. write (result );}