File stream read/write, large file move FileStream StreamWriter

Source: Internet
Author: User

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

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.