? while printing heart symbol

♀尐吖头ヾ 提交于 2019-12-22 06:02:36

问题


Sorry for asking a stupid question, We are trying to print heart symbol from database to Java XML file. But the same is getting printed as "?" not sure where am I missing. Have tried the char unicode. As a practice I tried it using in main method. Please find the sample java class.

public static void main(String[] args) {

    String t = "\u2665";
    String myUnicodeSymbol = "\u05D0";
    char hollowHeart = '\u2661';
    String hollowDiamond = "\u2662";
    String spade = "\u2660";
    String club = "\u2663";

    StringBuffer buffer = new StringBuffer("<HEAD>");
    buffer.append("<HEART>").append(hollowHeart).append("</HEART>");
    buffer.append("</HEAD>");
    System.out.println(t);
    System.out.println(buffer.toString());
}

The ouput is:- ? ?

Not sure what am I missing.


回答1:


I think it depends on settings of the console where the output lands.

I have compiled and run your code in Eclipse and have seen heart symbol. But if I run the program from standard windows console then I see "?" instead of heart symbol too.

However you need to change standard settings in Eclipse.

Window -> Preferences -> General -> Workspace -> set "Text file encoding" to "Other: UTF-8"



回答2:


System.out.println does not support I think the Unicode characters in MAC.

Reference From MACWORLD

Try this

PrintStream out = new PrintStream(System.out, true, "UTF-8");

out.println(t);

You also will need to throw UnsupportedEncodingException or handle it.



来源:https://stackoverflow.com/questions/9100125/while-printing-heart-symbol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!