Java:如何检测(和更改?)System.console的编码? [英] Java: How to detect (and change?) encoding of System.console?

查看:112
本文介绍了Java:如何检测(和更改?)System.console的编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序在控制台上运行,其Umlaut和其他特殊字符正在Mac上输出。这里有一个简单的测试程序:

I have a program which runs on a console and its Umlauts and other special characters are being output as ?'s on Macs. Here's a simple test program:

public static void main( String[] args ) {
    System.out.println("höhößüä");
    System.console().printf( "höhößüä" );
}



在默认的Mac控制台(默认UTF-8编码) :

On a default Mac console (with default UTF-8 encoding), this prints:

 h?h????
 h?h????

但是在手动将Mac终端的编码设置为Mac OS Roman后, >

But after manually setting the Mac terminal's encoding to "Mac OS Roman", it correctly printed

 höhößüä
 höhößüä

请注意,在使用System.console()的Windows系统上:

Note that on Windows systems using System.console() works:

 h÷h÷▀³õ
 höhößüä

那么如何让我的程序... rolleyes ...run everywhere?

So how do I make my program...rolleyes..."run everywhere"?

推荐答案

Epaga:看起来不错此处。您可以在printstream中设置输出编码 - 只需确定或确定要设置的输出编码。

Epaga: have a look right here. You can set the output encoding in a printstream - just have to determine or be absolutely sure about which is being set.

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

public class Test {
    public static void main (String[] argv) throws UnsupportedEncodingException {
    String unicodeMessage =
    "\u7686\u3055\u3093\u3001\u3053\u3093\u306b\u3061\u306f";

    PrintStream out = new PrintStream(System.out, true, "UTF-8");
    out.println(unicodeMessage);
  }
}

要确定控制台编码,您可以使用system命令locale并解析在德语UTF-8系统上的输出:

To determine the console encoding you could use the system command "locale" and parse the output which - on a german UTF-8 system looks like:

LANG="de_DE.UTF-8"
LC_COLLATE="de_DE.UTF-8"
LC_CTYPE="de_DE.UTF-8"
LC_MESSAGES="de_DE.UTF-8"
LC_MONETARY="de_DE.UTF-8"
LC_NUMERIC="de_DE.UTF-8"
LC_TIME="de_DE.UTF-8"
LC_ALL=

这篇关于Java:如何检测(和更改?)System.console的编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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