使用 Java 在控制台中显示印地语

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10950020/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:12:17  来源:igfitidea点击:

Display Hindi language in console using Java

javautf-8

提问by Sanjay Dutt

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());

File abc.txtcontains

文件abc.txt包含

\u0905\u092d\u0940 \u0938\u092e\u092f \u0939\u0948 \u091c\u0928\u0924\u093e \u091c\u094b \u091a\u093e\u0939\u0924\u0940 \u0939\u0948 \u092

I want to dispaly in Hindi language in console using Java.

我想在控制台中使用 Java 以印地语显示。

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";

如果我只是像这样打印 String str="\u0905\u092d\u0940 \u0938\u092e\u092f \u0939\u0948 \u091c\u0928\u0924\u093e \u091c\u094b\u0903e\u094b\u0903e\u0903e \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.

然后它工作正常,但是当我尝试从文件中读取时它不起作用。

help me out.

帮帮我。

回答by theglauber

Use Apache Commons Lang.

使用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.)

(确保您的控制台设置为显示印地语(正确的字体等),并且控制台的编码与您的 Java 编码相匹配。上面的 Java 代码只是骨架。)

回答by adarshr

You should store the contents in the file as UTF-8encoded 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.

您应该将文件中的内容存储为UTF-8编码的印地语字符。例如,在您的情况下,它将是??? ??? ?? ???? ?? ????? ??. 也就是说,不是保存 unicode 转义,而是直接保存原始的印地语字符。然后,您可以像平常一样简单地阅读。

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?

您只需要确保您使用的编辑器使用 UTF-8 编码保存它。看到西班牙语字符没有正确显示吗?

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

否则,您必须将文件制作为.properties文件并使用java.util.Properties它来读取,因为它本质上提供了 unicode 转义支持。

Also read Reading unicode character in java

另请阅读在 java 中读取 unicode 字符