在Windows控制台中从Java代码问题打印出unicode [英] Printing out unicode from Java code issue in windows console

查看:482
本文介绍了在Windows控制台中从Java代码问题打印出unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows控制台中打印出一个unicode符号有问题。

I have got a problem with printing out a unicode symbol in the windows console.

这里是打印unicode符号值的java代码;

Here's the java code that prints out the unicode symbol value;

System.out.print("\u22A2 ");

当我在Eclipse中运行程序时,编码设置为UTF-8时,问题不存在,然而,当涉及到Windows控制台符号被替换为一个问号。

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.

以下是为了尝试克服这个问题,没有成功;

The following was done to try overcome this problem, with no success;


  • 将Windows控制台的字体更改为Lucida控制台。

  • Change the font of windows console to Lucida Console.

每次运行Windows控制台时,我将更改编码设置,即使用 chcp 65001

Every time I run windows console I will change the encoding settings, i.e. with the use of chcp 65001



    我试过几次的额外步骤是运行带有参数的java文件,即 java -Dfile.encoding = UTF-8 Filter (其中Filter是类的名称)

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)

推荐答案

,您还需要一个PrintStream / PrintWriter,它将打印的字符编码为UTF-8。

In additions to the steps you have taken, you also need a PrintStream/PrintWriter that encodes the printed characters to UTF-8.

不幸的是,Java设计人员选择使用所谓的 encoding,这在Windows下几乎总是不可用的 *)。因此,使用 System.out System.err 天真将使您的程序输出显示不同,取决于你在哪里运行它。

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.

*)这将是一些非标准的代码页承认在这个星球上。和AFAIK,如果你有一个德国键盘和一个德国OEM Windows,你想在你的家乡时区有日期和时间,没有办法说:但我想要UTF-8输入/输出我的CMD窗口。这是为什么我有双重的Ubuntu大部分时间启动的一个原因,它不言而喻的终端做UTF-8。

*) 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.

以下通常适用于我在JDK7中:

The following usually works for me in JDK7:

public static PrintWriter stdout = new PrintWriter(
    new OutputStreamWriter(System.out, StandardCharsets.UTF_8),
    true);

对于古老的Java版本,我替换 StandardCharsets.UTF_8 Charset.forName(UTF-8)

For ancient Java versions, I replace StandardCharsets.UTF_8 by Charset.forName("UTF-8")

这篇关于在Windows控制台中从Java代码问题打印出unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆