A summary of method selection for file reading and writing using C #

Source: Internet
Author: User

Small text files (100M or less) directly using the ReadAllText () and WriteAllText () methods of the File class
The interior of these two methods is essentially the writetoend () that encapsulates the StreamReader class of ReadToEnd () and the StreamWriter class,
The return values of both methods are of type string, so only text files can be read and written

Small text file single-line read-write with StreamReader and StreamWriter these two classes

Small non-text files are read and written using the file class's ReadAllBytes () and WriteAllBytes () and byte[] as a broker
The inside of these two methods is actually encapsulating the FileStream's read () and write () methods,
The return values of both methods are byte arrays, so you can read and write any file

Large file
However, file read and write Jumbo file is error, because it is essentially using FileStream to read and write, but its byte[] size is written dead (that is, the size of the file is read and write), so it is to read or write the file all at once, the file is very large often cause memory overflow
So we have to use the data flow FileStream, because we can define the size of byte[] freely, to ensure that the memory does not overflow

Generally speaking, read files with FileMode.Open, write files with FileMode.Create
The Position property of the FileStream is a pointer to the location where the file stream is currently read and written
Code implementation:

1 using(FileStream fsread =NewFileStream (@"D:\Names.txt", FileMode.Open))2 {3 using(FileStream fswrite =NewFileStream (@"D:\temp.txt", FileMode.Create))4 {5 byte[] arr =New byte[ $];6 //record how many bytes of data were read7 intCount =0;8  while(Fsread.position <fsread.length)9 {Ten //every time you read,. Returns the number of bytes actually read, with Count records (may count less than $ after the last read) OneCount = Fsread.read (arr,0, arr. Length); A //writes the data in the array to the specified file -Fswrite.write (arr,0, count); - } the } -MessageBox.Show ("OK"); -}

Summarize:

First look at the size of the file-if it is a large file with FileStream, if it is a small file to see whether it is a text file-non-text files are used in the file class Readallbytes/writeallbytes, is a text file to see whether it is a single-line read and write- Is the StreamReader class/streamwriter class, not the readalltext/writealltext of the file class

In addition, any type of file can be read and written using FileStream.

A summary of method selection for file reading and writing using C #

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.