Streamreader reading text file garbled

Source: Internet
Author: User

Solve the Problem of garbled characters in reading text files. I used to determine the text file format before reading.

Streamreader sr = new streamreader (fullfilename, getfileencodetype (fullfilename ))

Public System. text. encoding getfileencodetype (string filename) {system. io. filestream FS = new system. io. filestream (filename, system. io. filemode. open, system. io. fileaccess. read); system. io. binaryreader BR = new system. io. binaryreader (FS); byte [] buffer = BR. readbytes (2); If (buffer [0]> = 0xef) {If (buffer [0] = 0xef & buffer [1] = 0xbb) {return system. text. encoding. utf8;} else if (buffer [0] = 0xfe & buffer [1] = 0xff) {return system. text. encoding. bigendianunicode;} else if (buffer [0] = 0xff & buffer [1] = 0xfe) {return system. text. encoding. unicode;} else {return system. text. encoding. default ;}} if (buffer [0] = 0x3c) // UTF-8 no Bom format {return system. text. encoding. utf8;} else {return system. text. encoding. default ;}}

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.