从 Windows 控制台中的 Java 代码问题打印出 unicode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20386335/
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
Printing out unicode from Java code issue in windows console
提问by Adrian
I have got a problem with printing out a unicode symbol in the windows console.
我在 Windows 控制台中打印出 unicode 符号时遇到问题。
Here's the java code that prints out the unicode symbol value;
这是打印出unicode符号值的java代码;
System.out.print("\u22A2 ");
The problem doesn't exist when I run the program in Eclipse with encoding settings as UTF-8, however when it comes to windows console the symbol gets replaced by a question mark.
当我在 Eclipse 中以 UTF-8 编码设置运行程序时,问题不存在,但是当涉及到 Windows 控制台时,符号被问号替换。
The following was done to try overcome this problem, with no success;
为了克服这个问题,做了以下工作,但没有成功;
Change the font of windows console to Lucida Console.
Every time I run windows console I will change the encoding settings, i.e. with the use of
chcp 65001
将 windows 控制台的字体更改为 Lucida 控制台。
每次运行 Windows 控制台时,我都会更改编码设置,即使用
chcp 65001
An extra step I've tried a few times was running the java file with an argument, i.e. java -Dfile.encoding=UTF-8 Filter
(where "Filter" is name of the class)
我试过几次的额外步骤是运行带有参数的 java 文件,即java -Dfile.encoding=UTF-8 Filter
(其中“过滤器”是类的名称)
回答by Ingo
In additions to the steps you have taken, you also need a PrintStream/PrintWriter that encodes the printed characters to UTF-8.
除了您采取的步骤之外,您还需要一个 PrintStream/PrintWriter 将打印的字符编码为 UTF-8。
Unfortunately, Java designers have chosen to open the standard streams with the so called "default" encoding, which is almost always unusable*)under Windows. Hence, using System.out
and System.err
naively will make your program output appear differently, depending on where you run it. This is straight against the goal: compile once, run anywhere.
不幸的是,Java 设计者选择使用所谓的“默认”编码打开标准流,这在 Windows 下几乎总是无法使用*)。因此,天真地使用System.out
和System.err
将使您的程序输出显示不同,具体取决于您运行它的位置。这与目标背道而驰:编译一次,在任何地方运行。
*)It will be some non standard "code page" nobody except Microsoft recognizes on this planet. And AFAIK, if for example you have a German keyboard and a "German" OEM Windows and you want to have date and time in your home time zone, there is just no way to say: But I want UTF-8 input/output in my CMD window. This is one reason why I have my dual Ubuntu booted most of the time, where it goes without saying that the terminal does UTF-8.
*)这将是一些非标准的“代码页”,除了微软在这个星球上没有人承认。而且 AFAIK,例如,如果您有一个德语键盘和一个“德语”OEM Windows,并且您希望在您的家乡时区有日期和时间,那么就没有办法说:但是我想要 UTF-8 输入/输出我的 CMD 窗口。这就是为什么我大部分时间都启动双 Ubuntu 的原因之一,不用说终端执行 UTF-8。
The following usually works for me in JDK7:
以下通常适用于 JDK7:
public static PrintWriter stdout = new PrintWriter(
new OutputStreamWriter(System.out, StandardCharsets.UTF_8),
true);
For ancient Java versions, I replace StandardCharsets.UTF_8
by Charset.forName("UTF-8")
对于古老的 Java 版本,我替换StandardCharsets.UTF_8
为Charset.forName("UTF-8")
回答by Hatem Badawi
For the Arabic language I used the following code:
对于阿拉伯语,我使用了以下代码:
PrintWriter stdout = new PrintWriter(
new OutputStreamWriter(System.out,StandardCharsets.ISO_8859_1),true);
回答by spider
By default, the code-page using in the CMDof Windows is 437. You can test by run this command in the prompt:
默认情况下,Windows的CMD中使用的代码页是437。您可以通过在提示符下运行此命令来进行测试:
C:\>chcp
Active code page: 437
And, this code-page prevent you from showing Unicode characters properly! You have to change code page to 65001AND using -Dfile.encoding=UTF-8for that purpose.
而且,此代码页会阻止您正确显示 Unicode 字符!为此,您必须将代码页更改为65001并使用-Dfile.encoding=UTF-8。
C:\>chcp 65001
Active code page: 65001
C:\>java -jar -Dfile.encoding=UTF-8 path/to/your/runnable/jar