The difference between StreamReader and Streamwrite and FileStream

Source: Internet
Author: User

The specific usage is not to repeat.

Record the difference in usage

StreamReader:

New FileStream (@ "D:\Readme.txt", FileMode.Open);   New StreamReader (FS);  

Streamwrite:

New FileStream (@ "D:\a.txt", filemode.createnew);   New StreamWriter (Fs,encoding.utf8);  

Filesteam:

FileStream fs1=New  Filesteam (path,filemode.open,fileaccess.read); Fs.read (...) FileStream FS2=new  Filesteam (path,filemode.open,fileaccess.write); Fs.write (...)

FileStream and Streamxxxx have read, write methods but where are their differences?

    • The biggest difference is that Streamreader/streamwriter is manipulating character data (char), and FileStream is manipulating byte data (byte), The default encoding for the FileStream and Streamxxxx classes is UTF8, and a Chinese character is 2 characters, so the Streamxxxx class is often used for opening and saving text, while FileStream is used for data transfer.
    • FileStream is not able to specify the encoding (because it sees only the binary form of the file, of course it does not matter coding), so if there is Chinese text, the need to transcode.
    • FileStream is a lower-level class that can simply read a file to a buffer, while the Streamxxxx class encapsulates some advanced methods, such as ReadLine () (read by row)
    • FileStream can specify FileMode, FileAccess, FileShare, fileoptions and other file access control permissions, share permissions, etc., greatly expanded the flexibility of file reading and writing, And Filestreamfilestream also provides the Beginread/beginwrite (asynchronous read-write) operation method

The difference between StreamReader and Streamwrite and FileStream

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.