File FileStream StreamReader StreamWriter C #

Source: Internet
Author: User

There are various IO devices, such as the file class (string and binary), which can be used to read and write files directly using the file class.

These various IO reads and writes are realized through the stream, the base class is stream, for a variety of different IO devices, there is filestream,memorystream.

FileStream can be implemented in any format, any size file read, but its method can only read bytes, not directly read the string or read a line, etc., the operation of the lower level.

The specialized read-write reader and writer for various streams are primarily used to facilitate the reading and writing of text and strings, which can be used not only to read bytes, but also to read a string or read a line directly. The constructor parameters of these read-write classes can be either a corresponding stream object or a corresponding file path. For example, text,string,stream,http have a corresponding read-write class.

File

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
But using file to read and write large files is an 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 stream FileStream, because we can freely define the size of byte[], to ensure that memory does not overflow.

StreamReader, StreamWriter

The position in the BaseStream does not accurately reflect the current position, and it is found that the position is increased by 2048 for a stride, so it is understood that the StreamReader took the buffer. By finding discovery in the. NET Framework SDK, you can specify the buffer size for StreamReader by using the constructor, but the minimum value is 128, which means that the StreamReader buffer cannot be disabled.

Read and write files how to choose the right class to operate

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.

Any type of file can be read and written using FileStream.

File FileStream StreamReader StreamWriter 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.