[英]Why is this Java program gives incorrect results on Eclipse and correct results when run from terminal?
Consider the following program. 考虑以下程序。
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
public class HelloWorld {
public static void main(String[] args) {
System.out.println(Charset.defaultCharset());
char[] array = new char[3];
array[0] = '\u0905';
array[1] = '\u0905';
array[2] = '\u0905';
CharBuffer charBuffer = CharBuffer.wrap(array);
Charset utf8 = Charset.forName("UTF-8");
ByteBuffer encoded = utf8.encode(charBuffer);
System.out.println(new String(encoded.array()));
}
}
When I execute this using terminal, 当我使用终端执行此操作时,
java HelloWorld
I get properly encoded, shaped text. 我得到正确编码的变形文本。 Default encoding was
MacRoman
. 默认编码为
MacRoman
。
Now when I execute the same code from Eclipse, I see incorrect text getting printed to the console. 现在,当我从Eclipse执行相同的代码时,我看到不正确的文本被打印到控制台。
When I change the file encoding option of Eclipse to UTF-8
, it prints correct results in Eclipse. 当我将Eclipse的文件编码选项更改为
UTF-8
,它将在Eclipse中打印正确的结果。
I am wondering why this happens? 我想知道为什么会这样吗? Ideally, file encoding options should not have affected this code because here I am using UTF-8 explicitly.
理想情况下,文件编码选项应该不会影响此代码,因为在这里我明确使用UTF-8。
Any idea why this is happening? 知道为什么会这样吗?
I am using Java 1.6 (Sun JDK), Mac OSx 10.7. 我正在使用Java 1.6(Sun JDK),Mac OSx 10.7。
You need to specify what encoding you want to use when creating the string: 您需要指定创建字符串时要使用的编码:
new String(encoded.array(), charset)
otherwise it will use the default charset. 否则它将使用默认字符集。
Make sure the console you use to display the output is also encoded in UTF-8. 确保用于显示输出的控制台也以UTF-8编码。 In Eclipse for example, you need to go to Run Configuration > Common to do this.
例如,在Eclipse中,您需要转到Run Configuration> Common来执行此操作。
System.out.println("\u0905\u0905\u0905");
would be the straight-forward usage. 直接使用。
And encoding is missing for the String constructor, defaulting to the set default encoding. 而且String构造函数缺少编码,默认为设置的默认编码。
new String(encoded.array(), "UTF-8")
This happens because Eclipse uses the default ANSI encoding, not UFT-8. 发生这种情况是因为Eclipse使用默认的ANSI编码,而不是UFT-8。 If your using a different encoding than what your IDE is using, you will get unreadable results.
如果您使用的编码与IDE使用的编码不同,那么您将得到难以理解的结果。
you need to change your console run configuration. 您需要更改控制台运行配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.