Print unicode character in java

Redone picture Redone · Jul 3, 2017 · Viewed 27.3k times · Source

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.

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.

Answer

Razib picture Razib · Jul 3, 2017

try to use utf8 character set -

        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