【发布时间】:2013-03-19 13:13:30
【问题描述】:
我有以下代码
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(Charset.defaultCharset().toString());
String accentedE = "é";
String utf8 = new String(accentedE.getBytes("utf-8"), Charset.forName("UTF-8"));
System.out.println(utf8);
utf8 = new String(accentedE.getBytes(), Charset.forName("UTF-8"));
System.out.println(utf8);
utf8 = new String(accentedE.getBytes("utf-8"));
System.out.println(utf8);
utf8 = new String(accentedE.getBytes());
System.out.println(utf8);
}
上面的输出如下
windows-1252
é
?
é
é
有人可以帮我理解这是做什么的吗?为什么会有这个输出?
【问题讨论】:
-
要获得预期的输出,请确保您为文件设置了文件编码类型“UTF8”。如果您使用的是 eclipse 右键单击文件选择属性并选择 utf8 作为文本文件编码类型。