. NET Learning notes----2015-06-25 (file stream filestream)

Source: Internet
Author: User

File stream

The process of creating a file stream object is written in the using, which automatically helps us to release the resources occupied by the stream.

            //FileStream of the operation Byte//StreamReader and StreamWriter manipulate characters.FileStream Fsread =NewFileStream (@"C:\Users\Administrator\Desktop\new.txt", FileMode.OpenOrCreate, FileAccess.Read); byte[] buffer =New byte[1024x768*1024x768*5]; //3.8M 5M//returns the number of valid bytes actually read this time            intr = Fsread.read (buffer,0, buffer.            Length); //decodes each element in a byte array into a string in the specified encoding format            strings = Encoding.UTF8.GetString (buffer,0, R); //Close the streamFsread.close (); //releasing resources occupied by a streamFsread.dispose ();            Console.WriteLine (s);            Console.readkey (); //using FileStream to write data//using (FileStream fswrite = new FileStream (@ "C:\Users\Administrator\Desktop\new.txt", FileMode.OpenOrCreate, FileAccess.Write))//{            //string str = "See if I have the wood to cover you out"; //byte[] buffer = Encoding.UTF8.GetBytes (str); //fswrite.write (buffer, 0, buffer.)            Length); //}            //Console.WriteLine ("OK"); //Console.readkey ();

Using file streams for multimedia file replication

Ideas:

Read the multimedia file you want to copy, and then write it to the location you specified.

usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_08_ using a file stream to replicate a multimedia file {classProgram {Static voidMain (string[] args) {            stringSource =@"D:\Csharp Tutorial \09-read-write files for object-oriented polymorphic \2, file classes. avi"; stringtarget =@"C:\Users\Administrator\Desktop\File. avi for Class";            CopyFile (source, target); Console.WriteLine ("Replication succeeded");        Console.readkey (); }         Public Static voidCopyFile (stringSoucre,stringtarget) {            //1. Create a stream responsible for reading the file            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 larger, it is read by looping through the                     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); }                }            }        }    }}

. NET Learning notes----2015-06-25 (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.