When we use system. io. when streamreader reads TXT files containing Chinese characters, it often reads garbled characters (streamwriater also has a similar problem in writing text files). The reason is very simple: the file encoding (encoding) it does not correspond to the encoding of streamreader/writer.
To solve this problem, I wrote a class to obtain the encoding of a text file. In this way, we can create the corresponding streamreader and streamwriter for read and write, so as to ensure that no garbled characters will occur. In fact, the principle is very simple. When a text editor (such as the notepad that comes with XP) generates a text file, if the encoding format is inconsistent with the default encoding format of the system (gb2312 by default in the Chinese system, A specific "encoding byte sequence identifier (encoding bit order madk, abbreviated as BOM)" will be added at the beginning of the TXT file, similar to the "MZ" file header in PE format. In this way, you can determine the encoding used when the text file is generated based on the BOM. We use Notepad for this Bom.ProgramIt is invisible by default, but it can be read when stream is used for byte reading. My txtfileencoding class is based on this Bom "File Header" to determine the encoding used when the TXT file is generated.
Using System; Using System. IO; Using System. text; Namespace XXX. Common { Public Class Encodinghelper { /// <Summary> /// Getencoding /// </Summary> /// <Param name = "FILENAME"> </param> /// <Returns> </returns> Public Static Encoding getencoding ( String Filename ){ Return Getencoding (filename, encoding. Default );} /// <Summary> /// Getencoding /// </Summary> /// <Param name = "stream"> </param> /// <Returns> </returns> Public Static Encoding getencoding (filestream stream ){ Return Getencoding (stream, encoding. Default );} /// <Summary> /// Getencoding /// </Summary> /// <Param name = "FILENAME"> </param> /// <Param name = "defaultencoding"> </param> /// <Returns> </returns> Public Static Encoding getencoding ( String Filename, encoding defaultencoding ){ VaR FS = New Filestream (filename, filemode. Open ); VaR Targetencoding = Getencoding (FS, defaultencoding); FS. Close (); Return Targetencoding ;} /// <Summary> /// Getencoding /// </Summary> /// <Param name = "stream"> </param> /// <Param name = "defaultencoding"> </param> /// <Returns> </returns> Public Static Encoding getencoding (filestream stream, encoding defaultencoding ){ VaR Targetencoding = Defaultencoding; If (Stream! = Null & Amp; stream. Length & gt; = 2 ){ // Saves the first 4 bytes of the file stream Byte Byte1 = 0 ; Byte Byte2 = 0 ; Byte Byte3 = 0 ; Byte Byte4 = 0 ; // Save the current seek location Long Origpos = stream. Seek ( 0 , Seekorigin. Begin); stream. Seek ( 0 , Seekorigin. Begin ); Int Nbyte = Stream. readbyte (); byte1 = Convert. tobyte (nbyte); byte2 = Convert. tobyte (stream. readbyte ()); If (Stream. length> = 3 ) {Byte3 =Convert. tobyte (stream. readbyte ());} If (Stream. length> = 4 ) {Byte4 = Convert. tobyte (stream. readbyte ());} // Determine Encoding Based on the first 4 bytes of the file stream // Unicode {0xff, 0xfe }; // Be-Unicode {0xfe, 0xff }; // Utf8 = {0xef, 0xbb, 0xbf }; If (Byte1 = 0xfe & Byte2 = 0xff ) // Unicodebe {Targetencoding = Encoding. bigendianunicode ;} If (Byte1 = 0xff & Byte2 = 0xfe & Byte3! = 0xff )// Unicode {Targetencoding = Encoding. Unicode ;} If (Byte1 = 0xef & Byte2 = 0xbb & Byte3 = 0xbf ) // Utf8 {Targetencoding = Encoding. utf8 ;} // Restore the seek location Stream. Seek (origpos, seekorigin. Begin );} Return Targetencoding ;} /// <Summary> /// Getencoding /// </Summary> /// <Param name = "bytes"> </param> /// <Param name = "defaultencoding"> </param> /// <Returns> </returns> Private Static Encoding getencoding ( Byte [] Bytes, encoding defaultencoding ){ VaR Targetencoding = Defaultencoding; Byte Byte1 = 0 ; Byte Byte2 =0 ; Byte Byte3 = 0 ; If (Bytes. length> = 1 ) {Byte1 = Bytes [ 0 ];} If (Bytes. length> = 2 ) {Byte2 = Bytes [ 1 ];} If (Bytes. length> = 3 ) {Byte3 = Bytes [ 2 ];} If (Byte1 = 0xfe & Byte2 = 0xff ) {Targetencoding = Encoding. bigendianunicode ;} If (Byte1 =0xff & Byte2 = 0xfe & Byte3! = 0xff ) {Targetencoding = Encoding. Unicode ;} If (Byte1 = 0xef & Byte2 = 0xbb & Byte3 = 0xbf ) {Targetencoding = Encoding. utf8 ;} Return Targetencoding ;}}}