About FileStream reading large file issues

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

In general, read files with FileMode.Open, write files with the Filemode.create,filestream position attribute is a pointer to mark the current location of the file stream read and write
Code implementation:

1 using (FileStream fsread = new FileStream (@ "D:\Names.txt", FileMode.Open)) 2 {3 using (FileStream fswrite = new Filestr EAM (@ "D:\temp.txt", FileMode.Create)) 4 {5 byte[] arr = new byte[200]; 6//record exactly how many bytes of data were read 7 int count = 0; 8 while (Fsrea D.position < Fsread.length) 9 {10//per read,. Returns the number of bytes actually read, using the Count record (which may count after the last read) by Count = Fsread.read (arr, 0, arr). LENGTH); 12//writes the data in the array to the specified file Fswrite.write (arr, 0, Count);}15}16 MessageBox.Show ("OK"); 17}

Knot

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.

About FileStream reading large file issues

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.