使用Java在控制台中显示印地语 [英] Display Hindi language in console using Java
问题描述
StringBuffer contents=new StringBuffer();
BufferedReader input = new BufferedReader(new FileReader("/home/xyz/abc.txt"));
String line = null; //not declared within while loop
while (( line = input.readLine()) != null){
contents.append(line);
}
System.out.println(contents.toString());
文件 abc.txt
包含
\u0905\u092d\u0940 \u0938\u092e\u092f \u0939\u0948 \u091c\u0928\u0924\u093e \u091c\u094b \u091a\u093e\u0939\u0924\u0940 \u0939\u0948 \u092
我想在使用Java的控制台中用印地语显示它。
I want to dispaly in Hindi language in console using Java.
如果我只是打印这样的
String str =\ u0905 \ u092d \ u0940 \ u0938 \ u092e \ u092f \ u0939 \ u0948 \ u091c \ u0928 \ u0924 \ u093e \ u091c \ u094b \\ \\ u091a \ u093e \ u0939 \ u0924 \ u0940 \ u0939 \ u0948 \ u092;
if i simply print like this String str="\u0905\u092d\u0940 \u0938\u092e\u092f \u0939\u0948 \u091c\u0928\u0924\u093e \u091c\u094b \u091a\u093e\u0939\u0924\u0940 \u0939\u0948 \u092";
System.out.println(str);
System.out.println(str);
然后它工作正常,但当我尝试从文件中读取它不起作用。
then it works fine but when i try to read from a file it doesn't work.
帮助我out。
推荐答案
使用 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);
(确保您的控制台设置为显示印地语(正确的字体等)和控制台的编码匹配你的Java编码。上面的Java代码只是简单的。)
(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.)
这篇关于使用Java在控制台中显示印地语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!