C # solution for reading and writing text files with garbled characters

Source: Internet
Author: User

When C # is used to read text files, if it contains Chinese characters, garbled characters often occur. The general solution is to add an Encoding in StreamReader. I am using Encoding. UTF8. Generally, this parameter can be used. However, this time I use UTF8 is invalid. In addition, unicode and UTF32 both use StreamReader's EndOfStream to convert to true after reading a row. I think the reason may be that the linefeed is not correctly read using this encoding, so that the last part of the file will be reached after one read.

Later, I saw a piece of information and used Encoding. GetEncoding ("gb2312") to solve the garbled problem. It seems that Baidu needs to look at the difference between gb2312 and UTF8.

 

Appendix: Original:

When reading a text file, if it contains Chinese characters, garbled characters may appear. The solution is simple. You only need to add an encoding to StreamReader when reading it. The Code is as follows:
Using System. IO;
StreamReader sr = new StreamReader (fileName, Encoding. GetEncoding ("gb2312"); // fileName indicates the absolute path of the file.

Since there are a lot of content on the Internet for C # operation files, I will not elaborate on them. The following two functions are provided to read and write text files respectively. Remember to reference System. IO;
/// <Summary>
/// Function:
/// Generate the file at the specified filename and fill it with content.
/// </Summary>
/// <Param name = "filename"> file name </param>
/// <Param name = "content"> file content </param>
Public static void CreateFile (string filename, string content)
{
Try
{
// The specific meaning of this constructor: at the specified file fileName, the second parameter indicates whether to append the content to the file. If the file exists and the second parameter append is false, the file will be rewritten. If the append is true, the content will be added to the end. If the file does not exist, the file will be created, and fill in the content. So even if the file does not exist, we can create a text file like this.
StreamWriter sw = new StreamWriter (filename, false, Encoding. GetEncoding ("gb2312 "));
Sw. Write (content );
Sw. Flush ();
Sw. Close ();
}
Catch (IOException ioe)
{
Throw ioe;
}
}

/// <Summary>
/// Function:
/// Add content at the beginning of the file
/// </Summary>
/// <Param name = "filename"> file name </param>
/// <Param name = "content"> inserted content </param>
Public static void InsertFile (string filename, string content)
{
Try
{
StreamReader sr = new StreamReader (filename, Encoding. GetEncoding ("gb2312 "));
Content + = sr. ReadToEnd ();
Sr. Close ();
CreateFile (filename, content );

}
Catch (IOException ioe)
{
Throw ioe;
}
}
/// <Summary>
/// Function:
/// Add the specified content at the end of the file
/// </Summary>
/// <Param name = "filename"> file name </param>
/// <Param name = "content"> add content </param>
Public static void AppendFile (string filename, string content)
{
Try
{
String filecontent = "";
StreamReader sr = new StreamReader (filename, Encoding. GetEncoding ("gb2312 "));
Filecontent + = sr. ReadToEnd () + content; // such

Related Article

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.