Display Hindi language in console using Java

前端 未结 2 1524
孤独总比滥情好
孤独总比滥情好 2020-12-20 23:13
StringBuffer contents=new StringBuffer();
BufferedReader input =  new BufferedReader(new FileReader(\"/home/xyz/abc.txt\"));
String line = null; //not declared withi         


        
相关标签:
2条回答
  • 2020-12-20 23:34

    Use Apache Commons Lang.

    import org.apache.commons.lang3.StringEscapeUtils;
    
    // open the file as ASCII, read it into a string, then
    String escapedStr; // = "\u0905\u092d\u0940 \u0938\u092e\u092f \u0939\u0948 ..."
    // (to include such a string in a Java program you would have to double each \)
    
    String hindiStr = StringEscapeUtils.unescapeJava( escapedStr );
    
    System.out.println(hindiStr);
    

    (Make sure your console is set up to display Hindi (correct fonts, etc) and the console's encoding matches your Java encoding. The Java code above is just the bare bones.)

    0 讨论(0)
  • 2020-12-20 23:36

    You should store the contents in the file as UTF-8 encoded Hindi characters. For instance, in your case it would be अभी समय है जनता जो चाहती है. That is, instead of saving unicode escapes, directly save the raw Hindi characters. You can then simply read like normal.

    You just have to make sure that the editor you use saves it using UTF-8 encoding. See Spanish language chars are not displayed properly?

    Otherwise, you'll have to make the file a .properties file and read using java.util.Properties as it offers unicode unescaping support inherently.

    Also read Reading unicode character in java

    0 讨论(0)
提交回复
热议问题