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

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

问题描述

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

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

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

  h?h ???? 
h?h ????但是,在将Mac终端的编码手动设置为Mac OS Roman之后,它正确地打印出来。

>

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

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

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

那么如何使我的程序... rolleyes ...无处不在?

解决方案

Epaga:看起来很正确这里。您可以在打印流中设置输出编码 - 只需确定或绝对确定要设置的输出。

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

public class Test {
public static void main(String [] argv)throws UnsupportedEncodingException {
String unicodeMessage =
\\\皆\\\さ\\\ん \\\、\\\こ\\\ん\\\に\\\ち\\\は;

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

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

  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 =


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ößüä" );
}

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

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

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

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

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

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

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

解决方案

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

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天全站免登陆