In the process of writing a text file read/write program, the following code is provided:
Streamreader sr = new streamreader (filename );
The result shows garbled characters when opening a Chinese text file.
The reason is that since Windows 2000 and later operating systems use Unicode by default during file processing, the default file encoding for. NET is also Unicode. Unless otherwise specified, streamreader is encoded as Unicode by default, rather than the ANSI code page of the current system. However, most documents are still stored in ANSI encoding, and Chinese text uses gb2312, which causes Chinese garbled characters. That is to say, you must specify the encoding format when reading the text.
But the problem is, system. Text. Encoding inside a pile of encoding format ASCII, UTF-8, etc., which one to choose?
It is actually very simple.
System. Text. encoding. Default tells
Streamreader can be encoded in the current operating system.
Streamreader reader = new streamreader (filename, system. Text. encoding. Default)