更新:发现十进制数字 169 是 Unicode 和 Windows-1252 中的版权符号字符后,我感到很困惑。所以我不知道到底发生了什么!
如果代码对任何试图解开这个谜团的人有帮助,我将保持原样。
可能是由于有限的(非Unicode)字符集和以任何方式生成输出时使用的编码。
这里是演示代码,显示您的示例字符串通过System.out 转储到控制台,使用当前默认Charset,使用UTF-8,并使用有限的legacy Windows-1252。
请参阅此示例code run live at IdeOne.com。
import java.util.*;
import java.lang.*;
import java.io.*;
import java.nio.charset.StandardCharsets ;
import java.nio.charset.Charset ;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String blurb = "© 2021 ABC Inc. All rights reserved." ;
// The character set and encoding currently in use by `System.out` is not known, some default.
System.out.println( "----------| default |--------------------------" );
System.out.println( "blurb: " + blurb ) ;
// Let's set the character set and encoding to UTF-8 by wrapping `System.out` in a `PrintStream`.
System.out.println( "----------| UTF-8 |--------------------------" );
try
{
PrintStream printStream = new PrintStream( System.out , true , StandardCharsets.UTF_8.name() );
printStream.println( "blurb: " + blurb );
}
catch ( UnsupportedEncodingException e )
{
e.printStackTrace();
}
// In contrast, try Windows-1252 character set.
System.out.println( "----------| windows-1252 |--------------------------" );
// Verify windows-1252 charset is available on the current JVM.
String windows1252CharSetName = "windows-1252";
boolean isWindows1252CharsetAvailable = Charset.availableCharsets().keySet().contains( windows1252CharSetName );
if ( isWindows1252CharsetAvailable )
{
System.out.println( "isWindows1252CharsetAvailable = " + isWindows1252CharsetAvailable );
} else
{
System.out.println( "FAIL - No charset available for name: " + windows1252CharSetName );
}
// Print the blurb.
try
{
PrintStream printStream = new PrintStream( System.out , true , windows1252CharSetName );
printStream.println( "blurb: " + blurb );
}
catch ( UnsupportedEncodingException e )
{
e.printStackTrace();
}
}
}
运行时。
----------| default |--------------------------
blurb: © 2021 ABC Inc. All rights reserved.
----------| UTF-8 |--------------------------
blurb: © 2021 ABC Inc. All rights reserved.
----------| windows-1252 |--------------------------
isWindows1252CharsetAvailable = true
blurb: � 2021 ABC Inc. All rights reserved.
正如预期的那样,我们看到COPYRIGHT SIGN 字符(十进制代码点 169)在 Unicode 中正确显示,但在 Windows-1252 中失败。 According to Wikipedia,
推荐阅读:The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)