File stream read/write
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;namespace_09 file Stream {classProgram {Static voidMain (string[] args) { //string msg = "3,000 feet straight down"; ////String to byte array //byte[] buffer = System.Text.Encoding.UTF8.GetBytes (msg); ////byte array to string //string str= System.Text.Encoding.UTF8.GetString (buffer); //writes a string to a file, writes the content in a stream//using (FileStream fs = new FileStream ("1.txt", FileMode.Create, FileAccess.Write))//{ //string msg = "The text can be a pen to control Lori"; //byte[] buffer = System.Text.Encoding.UTF8.GetBytes (msg); //FS. Write (buffer, 0, buffer. Length); //}//Console.readkey (); //FS. Close ();//Close the stream//FS. Flush ();//Clear Buffer//FS. Dispose ();//frees up the resources that are occupied (these three must be written together, using the same method instead of the three freed resources)//read data in a streaming way using(FileStream fs =NewFileStream ("1.txt", FileMode.Open, FileAccess.Read)) { byte[] buffer =New byte[FS. Length]; Fs. Read (Buffer,0, buffer. Length); stringmsg =System.Text.Encoding.UTF8.GetString (buffer); Console.WriteLine (msg); } console.readkey (); } }}
Large file movement
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;namespace_10 Large file Move {classProgram {Static voidMain (string[] args) { //Read the stream using(FileStream fsread=NewFileStream (@"g:\ video \ Pirate. mkv", FileMode.Open, FileAccess.Read)) { //Write the Stream using(FileStream fswrite=NewFileStream (@"g:\ movie \ Pirate. mkv", FileMode.Create, FileAccess.Write)) { //the size of each read is 5M byte[]buffer=New byte[1024x768*1024x768*5]; //actual (the size actually read to) intR= fsread.read (Buffer,0, buffer. Length); while(r>0) { //WriteFswrite.write (Buffer,0, R); Console.WriteLine ("**"); //re-readr = Fsread.read (buffer,0, buffer. Length); }}} Console.WriteLine ("Okay, OK."); Console.readkey (); } }}
Another way to read and write
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;namespace_11 Another way of reading and writing {classProgram {Static voidMain (string[] args) { #regionReading data//using (StreamReader reader = new StreamReader ("1.txt", Encoding.default))//{ //only one row was read//string msg= Reader. ReadLine (); //string msg; //to Loop Read//While ((Msg=reader. ReadLine ())!=null)//{ //Console.WriteLine (msg); //} //always read to the end of the stream//string msg= Reader. ReadToEnd (); //Console.WriteLine (msg); //While (!reader. Endofstream)//{ //Console.WriteLine (reader. ReadLine ()); //} // } #endregion #regionWrite Data//using (StreamWriter write=new StreamWriter ("One.txt"))//{ //write. Write ("This can be the same"); //} #endregion //Console.readkey (); } }}
File stream read/write, large file move FileStream StreamWriter