将unicode打印到控制台 [英] Printing unicode to console

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

问题描述

我正在尝试创建一个可以将本地化消息打印到控制台的自定义打印流。我在Windows上遇到了这个问题。这是我正在尝试做的事情

I'm trying to create a custom print stream that can print localized messages to the console. I encountered a problem doing this on Windows. Here is what I'm attempting to do


  • 我有一个unicode字符串

  • 转换unicode字符串使用UTF-8编码的字节数

  • 使用控制台编码将字节转换为新字符串

  • 使用控制台编码将新字符串打印到控制台

  • I have a unicode string
  • Convert unicode string to bytes using UTF-8 encoding
  • Convert bytes to a new string with console encoding
  • Print new string to console with console encoding

在这段代码中,我尝试了上述步骤,但它失败了。奇怪的是,默认的System.out.println调用正常工作。但是,我想使用自定义打印流而不依赖于默认的System.out。

In this code, I tried to do the above steps but it fails miserably. Strangely the default System.out.println call works correctly. However, I want to use a custom print stream and not rely on the default System.out.

有人可以解释如何使用自定义打印流将unicode打印到控制台吗?为什么默认的System.out已经配备正确打印东西?

Can someone explain how I can print unicode to the console using my custom print stream? And why is the default System.out already equipped to print things correctly?

这是我的代码 - 我编译了它并从命令行运行它。我事先将我的系统区域设置设置为zh-CN。

Here is my code - I compiled it and ran it from the command line. I set my system locale to zh-CN beforehand.

public static void main(String[] args) throws Exception{
    Charset defaultCharset = Charset.defaultCharset();
    System.out.println(defaultCharset);
    // charset is windows-1252

    String unicodeMessage =
            "\u4e16\u754c\u4f60\u597d\uff01";

    System.out.println(unicodeMessage);
    // string is printed correctly using System.out (世界你好!)


    byte[] sourceBytes = unicodeMessage.getBytes("UTF-8");
    String data = new String(sourceBytes , defaultCharset.name());

    PrintStream out = new PrintStream(System.out, true, defaultCharset.name());
    out.println(data);
    // prints gibberish: ??–????????????
}


推荐答案

windows-1252 charset是问题在这里。我们需要使用UTF-8字符集进行打印。对于我以下工作:

windows-1252 charset is the problem here. We need to use UTF-8 charset to print. Following worked for me:

public static void main(String[] args) throws Exception{
    Charset utf8Charset = Charset.forName("UTF-8");
    Charset defaultCharset = Charset.defaultCharset();
    System.out.println(defaultCharset);
    // charset is windows-1252

    String unicodeMessage = "\u4e16\u754c\u4f60\u597d\uff01";

    System.out.println(unicodeMessage);
    // string is printed correctly using System.out (世界你好!)


    byte[] sourceBytes = unicodeMessage.getBytes("UTF-8");
    String data = new String(sourceBytes , defaultCharset.name());

    PrintStream out = new PrintStream(System.out, true, utf8Charset.name());
    out.println(data);
}

这篇关于将unicode打印到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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