为什么在调用.getBytes()时字符串"¿"会被翻译为"¿"

Ran*_*yaa 2 java utf-8 character-encoding

使用时将字符串"¿"写出来

System.out.println(new String("¿".getBytes("UTF-8")));
Run Code Online (Sandbox Code Playgroud)

¿写的而不仅仅是¿

为什么?我们如何解决它?

p3t*_*t0r 5

您不必使用UTF-16来解决此问题:

new String("¿".getBytes("UTF-8"), "UTF-8");
Run Code Online (Sandbox Code Playgroud)

工作得很好.只要给出的getBytes()方法编码与传递给String构造函数的编码相同,你应该没问题!