Java solves the problem of Chinese garbled code in reading and writing local files

Java solves the problem of Chinese garbled code in reading and writing local files

preface:

When using Java programs to read and write txt files containing Chinese, it often appears that the contents read or written will be garbled. The reason is actually very simple, that is, the system coding and program coding adopt different coding formats. Usually, if you don't modify it yourself, the encoding format adopted by windows itself is GBK (and GBK and GB2312 are basically the same encoding method). If you don't modify the encode in the IDE, it defaults to UTF-8 encoding, which is why there is chaos. When the txt file (GBK) manually created and written in OS is directly read by program (UTF-8), it will be garbled. In order to avoid possible garbled Chinese, it is best to explicitly specify the encoding format when writing and reading the file.

Read local files by line:

Write to local file:

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>