我有一個屬性文件,將德文字符映射到它們的十六進制值(00E4)。我必須用「iso-8859-1」編碼這個文件,因爲這是獲得德文字符顯示的唯一方法。我想要做的是通過德語單詞並檢查這些字符是否出現在字符串中的任何位置,以及它們是否用十六進制格式替換該值。例如用\u00E4
替換德國字符。Java不寫屬性文件「 u」
該代碼替換字符很好,但在一個反彈,我得到兩個像這樣\\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();
}
「我收到了兩個像這樣的」4「。這是一個錯字嗎?有一個。 – Michael
感謝您指出這一點,似乎你必須逃避這裏的反斜槓。但是,這是錯誤的,這意味着另一個反斜槓。 –