How to resolve GBK encoded files in Chinese to convert into UTF-8 encoded files and not garbled

Source: Internet
Author: User

First, we must make it clear why the normal conversion is garbled.

Because our data write is GBK write, and then display the words are displayed according to the file preservation situation, the front save the situation is GBK, consistent, so do not garbled, and later will save the situation into UTF-8, but the content of the write is GBK situation, encoding and decoding inconsistent, so garbled.

Our general approach to solving garbled characters is:

String S=new string (str.getbytes ("GBK"), "UTF-8");

The idea that I'm going to work on it is like this:

First of all we know the cause of garbled only because of Chinese, Chinese garbled way looks like the above solution.

The encoding of the file changes, and the contents of the coding also changed, it is clear that the file is not simply formatted to change the format and must change the contents of the file.

My initial idea:

Reads the contents of the file into the stream and writes to the destination file by the specified encoding.

Process:

Fetching the target file into the stream

Reads the information in the source file in the form of a string,

Writes a string to the destination file

Note the problem:

1, read the target file default read encoding is the current project encoding instead of the source file encoding, we can use the fileinputstream of the structure containing the encoding to meet our requirements

2. It is not necessary to convert the string format after reading each line of string. Just like I made a mistake, I will read the successful string in the form of a new string (GetBytes ("GBK"), "UTF-8"), the format of the file again, resulting in garbled, ignoring a problem, I read the file is actually decoded, The encoding of the string should be the way my project is encoded.

3, read a row of data remember the newline

The other problems are:

Changes the encoding format of all the specified files in a folder, and a recursive invocation of the specified method!

How to resolve GBK encoded files in Chinese to convert into UTF-8 encoded files and not garbled

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.