The problem of Chinese garbled characters displayed online in the txt file in java

Problem Description:

        When using Java programs to read and write txt files containing Chinese, it often occurs that the content read or written will appear garbled.

Solution principle:

        The reason is that the coding of the system and the coding of the program use different coding formats. Usually, if you don't modify it yourself, the encoding format used by windows itself is gbk (and gbk and gb2312 are basically the same encoding method), and if you don't modify the Encode in the IDE, the default is utf-8 encoding, that's why The reason for the garbled characters. When the txt file (gbk) manually created and written under the OS is read directly by the program (utf-8), it will be garbled. In order to avoid possible Chinese garbled characters, it is best to explicitly specify the encoding format when writing and reading files.

Encode the content of the obtained file:

    
					if (fileName.endsWith(".txt")) {
						content= new String(byte,"GBK");
					}else {
						content= new String(byte);
					}


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326720949&siteId=291194637