Use of FileStream files, filestream files

Source: Internet
Author: User

Use of FileStream files, filestream files

// FileStream // (for byte operations) a spoonful of water and a spoonful of files that can be operated in any format
// File is read at once

// Read text files

1 // FileMode. openOrCreate what operations do you perform on files? 2 // FileAccess. read File data parameter 3 // 1. create a FileStream object 4 FileStream fileRead = new FileStream (@ "C: \ Users \ wbrm \ Desktop \ new FileStream file .txt", FileMode. openOrCreate, FileAccess. read); 5 byte [] buffer = new byte [1024*1024*5]; 6 int r = fileRead. read (buffer, 0, buffer. length); // 0 indicates that the data is read from that byte // buffer. length Size 7 // decodes the encoded format of each element in the byte array into a string 8 string s = Encoding. default. getString (buffer, 0, r); 9 fileRead. close (); // Close stream 10 fileRead. dispose (); // release the resources occupied by the stream 11 Console. writeLine (s); 12 Console. readKey ();

Write a text file

1 // The written encoding format must be the same as the read encoding format. 2 // process the created file stream object in Using, it will automatically help us release the space occupied by 3 using (FileStream fswrite = new FileStream (@ "C: \ Users \ wbrm \ Desktop \ new Text Document (2).txt", FileMode. openOrCreate, FileAccess. write) 4 {5 string str = "check if I have overwritten you"; 6 byte [] buffer = Encoding. default. getBytes (str); // convert to a byte array 7 fswrite. write (buffer, 0, buffer. length); 8} 9 Console. writeLine ("successfully written"); 10 Console. readKey ();

Use File streams to replicate multimedia files

1 static void Main (string [] args) 2 {3 // train of thought: read the multimedia file to be copied first and then write it to your specified location 4 string courece = @ "C: \ Users \ wbrm \ Desktop \ for loop exercises. avi "; 5 string target = @" C: \ Users \ wbrm \ Desktop \ new. avi "; 6 CopyFile (courece, target); 7 Console. writeLine ("copied"); 8 Console. readKey (); 9} 10 public static void CopyFile (string courece, string target) 11 {12 // read stream 13 using (FileStream fsRead = new FileStream (courece, FileMode. openOrCreate, FileAccess. read) 14 {15 // write stream 16 using (FileStream fsWrite = new FileStream (target, FileMode. openOrCreate, FileAccess. write) 17 {18 byte [] buffer = new byte [1024*1024*5]; 19 // because the file may be large, when we read, read 20 while (true) 21 {22 // return the number of bytes actually read this time 23 int r = fsRead. read (buffer, 0, buffer. length); 24 // if 0 is returned, it means nothing is read. 25 if (r = 0) 26 {27 break; 28} 29 fsWrite is read. write (buffer, 0, r); // The maximum number of written bytes r30} 31} 32} 33}

 

Related Article

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.