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