. NET learning notes-several ways to read and write files

Source: Internet
Author: User

There are many useful libraries in. NET to read and write files on your hard disk. Commonly used are:

File:1. When to use: when the read and write pieces are small size, and can be read and write operations at the same time to use
2. Different ways to read and write file types are not the same.

ReadAllBytes () Writeallbyte (): can read and write any type of file (FileStream is encapsulated)
ReadAllLines () ReadAllText ():(inside package is Streamwriter,streamreader), can only read and write text type file

FileStream:
1. When reading and writing large files, it can customize the size of the read-write array
2. A flow can only do one thing: if you need to read and write then you need to create two streams
3. READ: Reading data to an array write is to write the data of the array back to the file (type conversion)

Use the FileStream class to read and write a movie example code:

1   //Read2             using(FileStream fsread =NewFileStream (@"F:\, Ms. Cang. avi", FileMode.Open))3             {4                 using(FileStream fswrite =NewFileStream (@"d:\ learning. avi", FileMode.Create))5                 {6                     //the length of the custom array7                     byte[] bytes =New byte[1024x768];8                     //A loop is required when the end of the file is not read9                      while(Fsread.position <fsread.length)Ten                     { One                         //when read, the Position property automatically changes, remembering where it is currently read, in bytes A                         //count can get the number of bytes currently read to -                         intCount = Fsread.read (Bytes,0, Bytes. Length); -                         if(Count = =0) the                         { -                              Break; -                         } -                         //Write +Fswrite.write (Bytes,0, count);//just write the number of bytes read to it. -                     } +                 } AMessageBox.Show ("OK"); at}

Streamreader/streamwrite:
       1. Read and write only to text files
        2. It can read and write by line

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.