C # File Stream FileStream

Source: Internet
Author: User

Low memory impact with FILESTREAM benefits

Reading data

Filestrame  fsread = new Filestrame (@ "C:\a.txt", FileMode.OpenOrCreate, FileAccess.Read);
byte[] buffer = new byte[1024*1024*5];//maximum read size
Returns the number of valid bytes actually read this time
int t = fsraad.read (buffer, 0, buffer. LENGTH);//decodes each element in a byte array into a string in the specified encoding format
string s = Encoding.Default.GetString (buffer,0,r);
Close the stream
Fsread.close ();
Fsread.dispose ();
Console.WriteLine (s);
Console.readkey ();

Write file

  Using FileStream to write data, use the using auto-release resource using (FileStream fswrite = new FileStream (@ "C:\Users\SpringRain\Desktop\new.txt", FileMode.OpenOrCreate, FileAccess.Write) {              string str = "See me as a nomad and cover you Up";              byte[] buffer = Encoding.UTF8.GetBytes (str);              Fswrite.write (buffer, 0, buffer.) Length);} Console.WriteLine ("Write OK"); Console.readkey ();

Implementing Multimedia File Replication

 Static voidMain (string[] args) {            //idea: The multimedia file that you want to copy is read out and then written to the location you specified            stringSource =@"C:\Users\SpringRain\Desktop\1, review. wmv"; stringtarget =@"C:\Users\SpringRain\Desktop\new.wmv";            CopyFile (source, target); Console.WriteLine ("Replication succeeded");        Console.readkey (); }         Public Static voidCopyFile (stringSoucre,stringtarget) {            //1. We create a stream that is responsible for reading            using(FileStream fsread =NewFileStream (Soucre, FileMode.Open, FileAccess.Read)) {                //2. Create a stream responsible for writing                using(FileStream fswrite =NewFileStream (Target, FileMode.OpenOrCreate, FileAccess.Write)) {                    byte[] buffer =New byte[1024x768*1024x768*5]; //because the file may be large, we should read it through a loop .                     while(true)                    {                        //returns the number of bytes actually read from this read                        intr = Fsread.read (buffer,0, buffer.                        Length); //If you return a 0, it means that nothing has been read.                        if(r = =0)                        {                             Break; } fswrite.write (Buffer,0, R); }                                 }            }        }

C # File Stream FileStream

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.