I'm trying to do something with transcode the string. But the result seem to be conflict with Java documentation. Why?

My environment is Windows 10 (chinese simplified), Intellij idea 2018.3.1, openjdk 11. For this system, its charset is 936 (that means GBK).

//open a inputstream of a utf8 file
InputStreamReader InputStream = new InputStreamReader(FileStream,"utf8");
BufferedReader BufferedRead = new BufferedReader(InputStream);
String Utf8String = BufferedRead.readLine();
byte [] bytetest1 = Utf8String.getBytes();
//print bytetest to console...
byte [] Utf8byte2 = Utf8String.getBytes("utf8");
//print UTF8byte2 to console...
byte [] GBKbyte3 = Utf8String.getBytes("gbk");
//print GBKbyte3 to console...

My system charset is gbk. So the result should be GBKbyte3 equal to bytetest1, but the actual result is UTF8byte2 equals to bytetest1.

Here is the output:

bytetest equal to UTF8byte2

0 Answers