【发布时间】:2017-12-19 06:31:30
【问题描述】:
我有一个将德语字符映射到其十六进制值 (00E4) 的属性文件。我不得不用“iso-8859-1”对这个文件进行编码,因为这是让德语字符显示的唯一方法。我要做的是检查德语单词并检查这些字符是否出现在字符串中的任何位置,以及它们是否确实用十六进制格式替换了该值。例如用\u00E4 替换德国字符。
代码很好地替换了字符,但在一次反冲中,我得到了两个像\\u00E4 这样的字符。您可以在我使用"\\u" 尝试打印\u 的代码中看到,但事实并非如此。关于我在哪里出错的任何想法?
private void createPropertiesMaps(String result) throws FileNotFoundException, IOException
{
Properties importProps = new Properties();
Properties encodeProps = new Properties();
// This props file contains a map of german strings
importProps.load(new InputStreamReader(new FileInputStream(new File(result)), "iso-8859-1"));
// This props file contains the german character mappings.
encodeProps.load(new InputStreamReader(
new FileInputStream(new File("encoding.properties")),
"iso-8859-1"));
// Loop through the german characters
encodeProps.forEach((k, v) ->
{
importProps.forEach((key, val) ->
{
String str = (String) val;
// Find the index of the character if it exists.
int index = str.indexOf((String) k);
if (index != -1)
{
// create new string, replacing the german character
String newStr = str.substring(0, index) + "\\u" + v + str.substring(index + 1);
// set the new property value
importProps.setProperty((String) key, newStr);
if (hasUpdated == false)
{
hasUpdated = true;
}
}
});
});
if (hasUpdated == true)
{
// Write new file
writeNewPropertiesFile(importProps);
}
}
private void writeNewPropertiesFile(Properties importProps) throws IOException
{
File file = new File("import_test.properties");
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
importProps.store(writer, "Unicode Translations");
writer.close();
}
【问题讨论】:
-
“我得到了两个这样的\u00E4。”那是错字吗?有一个。
-
感谢您指出这一点,看来您必须避开此处的反斜杠。但是是的,那是错误的,本来应该有另一个反斜杠。
标签: java encoding properties hex translation