java 在java中打印unicode字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44878530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print unicode character in java
提问by Redone
Displaying unicode
character in java shows "?" sign. For example, i tried to print "?". Its unicode
Number is U+0905 and html representation is "अ".
The below codes prints "?" instead of unicode
character.
unicode
在java中显示字符显示“?” 符号。例如,我试图打印“?”。其unicode
编号为 U+0905,html 表示为“अ”。下面的代码打印“?” 而不是unicode
性格。
char aa = '\u0905';
String myString = aa + " result" ;
System.out.println(myString); // displays "? result"
Is there a way to display unicode
character directly from unicode
itself without using unicode
numbers? i.e "?" is saved in file now display the file in jsp.
有没有办法unicode
直接从unicode
自身显示字符而不使用unicode
数字?IE ”?” 保存在文件中现在在jsp中显示文件。
回答by Razib
try to use utf8 character set -
尝试使用 utf8 字符集 -
Charset utf8 = Charset.forName("UTF-8");
Charset def = Charset.defaultCharset();
String charToPrint = "u0905";
byte[] bytes = charToPrint.getBytes("UTF-8");
String message = new String(bytes , def.name());
PrintStream printStream = new PrintStream(System.out, true, utf8.name());
printStream.println(message); // should print your character
回答by Ralf Kleberhoff
Your myString
variable contains the perfectly correct value. The problem must be the output from System.out.println(myString)
which has to send some bytes to some output to show the glyphs that you want to see.
您的myString
变量包含完全正确的值。问题必须是输出必须System.out.println(myString)
将一些字节发送到某些输出以显示您想要查看的字形。
System.out
is a PrintStream using the "platform default encoding" to convert characters to byte sequences - maybe your platform doesn't support that character. E.g. on my Windows 7 computer in Germany, the default encoding is CP1252, and there's no byte sequence in this encoding that corresponds to your character.
System.out
是使用“平台默认编码”将字符转换为字节序列的 PrintStream - 也许您的平台不支持该字符。例如,在我在德国的 Windows 7 计算机上,默认编码是 CP1252,并且此编码中没有与您的字符对应的字节序列。
Or maybe the encoding is correct, but simply the font that creates graphical glyphs from characters doesn't have that charater.
或者也许编码是正确的,但只是从字符创建图形字形的字体没有那个字符。
If you are sending your output to a Windows CMD.EXE window, then maybe both reasons apply.
如果您将输出发送到 Windows CMD.EXE 窗口,那么可能这两个原因都适用。
But be assured, your string is correct, and if you send it to a destination that can handle it (e.g. a Swing JTextField), it'll show up correctly.
但请放心,您的字符串是正确的,如果您将其发送到可以处理它的目的地(例如 Swing JTextField),它将正确显示。
回答by Mohammed Tamimi
Java defines two types of streams, byte and character.
Java 定义了两种类型的流,字节和字符。
The main reason why System.out.println() can't show Unicode characters is that System.out.println() is a byte stream that deal with only the low-order eight bits of character which is 16-bits.
System.out.println() 不能显示Unicode 字符的主要原因是System.out.println() 是一个字节流,只处理16 位字符的低8 位。
In order to deal with Unicode characters(16-bit Unicode character), you have to use character based stream i.e. PrintWriter.
为了处理 Unicode 字符(16 位 Unicode 字符),您必须使用基于字符的流,即 PrintWriter。
PrintWriter supports the print( ) and println( ) methods. Thus, you can use these methods in the same way as you used them with System.out.
PrintWriter 支持print() 和println() 方法。因此,您可以像使用 System.out 一样使用这些方法。
PrintWriter printWriter = new PrintWriter(System.out,true);
char aa = '\u0905';
printWriter.println("aa = " + aa);