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)