1

我正在尝试从 Eclipse 中的控制台获取输入。但是当我写像'ß'这样的特殊德语字符时,这个字符无法识别,它就像'??'。

我的代码如下。我试过Locale.GERMANY了,但没有用。

Scanner s = null;
Locale.setDefault(Locale.GERMAN);
System.out.println(Locale.getDefault());
System.setProperty("file.encoding", "UTF-8");

try {
    s = new Scanner(System.in);
    s.useLocale(Locale.GERMAN);

    System.out.println(s.locale());

    while (s.hasNext()) {
        System.out.println(s.next());
    }
} finally {
    if (s != null) {
        s.close();
    }
}
4

1 回答 1

2
s = new Scanner(System.in, "Windows-1252");

The constructor of Scanner has a constructor with encoding. The constructor without encoding uses the default platform encoding. That System.setProperty("file.encoding", "UTF-8"); evidently misled the Scanner to use UTF-8.

Clarification: System.in uses the OS encoding, hence Windows-1252 for the German locale.

于 2013-05-05T20:14:38.687 回答