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