【发布时间】:2015-08-07 14:36:24
【问题描述】:
所以我正在研究一个本地化示例以及使用ResourceBundle 的常规方法,但似乎所有内容都不支持 UTF-8,所以我将继续使用 Properties。
我已经得到了它的实际属性,但在西班牙语文件中,它不喜欢重音。我用 UTF-8 读取它,但它不在乎,只是显示与以前不同的符号。
输出:
íHola!
┐C¾mo estßs?
íAdi¾s!
预期输出:
¡Hola!
¿Cómo estás?
¡Adios!
属性文件:
greetings = ¡Hola!
farewell = ¡Adiós!
inquiry = ¿Cómo estás?
代码:
import java.util.*;
import java.io.*;
public class Test{
public static void main(String[] args) throws IOException {
String language;
String country;
if (args.length != 2) {
language = new String("en");
country = new String("GB");
} else {
language = new String(args[0]);
country = new String(args[1]);
}
String file = String.format("lang_%s_%s.properties",language,country);
InputStream utf8in = Test.class.getClassLoader().getResourceAsStream(file);
Reader reader = new InputStreamReader(utf8in, "UTF-8");
Properties props = new Properties();
props.load(reader);
System.out.println(props.getProperty("greetings"));
System.out.println(props.getProperty("inquiry"));
System.out.println(props.getProperty("farewell"));
}
}
我刚刚花了大约 40 分钟阅读我能找到的所有内容,它们要么与我现在得到的完全相同,要么略有不同,但在尝试时产生了相同的结果。
有人可以告诉我如何获得预期的输出吗?
【问题讨论】:
-
我能够重现它。它在带有 UTF-8 控制台设置的 Eclipse 中运行良好。不幸的是,我想不出将 Windows 控制台更改为 UTF-8 的方法。有趣的是“System.console().writer().println(props.getProperty("greetings"));”在 Windows 控制台上工作正常。也许这有帮助
-
我在 Eclipse 中使用 UTF-8 控制台设置进行了尝试,它显示了黑色的小菱形而不是字符。可以运行命令更改命令提示符,但它很不确定并且并不总是有效。另外,它要求您更改字体。我得看看那个sn-p。
标签: java character-encoding properties-file