简体   繁体   中英

Java not writing “\u” to properties file

I have a properties file that maps german characters to their hex value (00E4). I had to encode this file with "iso-8859-1" as it was the only way to get the german characters to display. What I'm trying to do is go through german words and check if these characters appear anywhere in the string and if they do replace that value with the hex format. For instance replace the german char with .

The code replaces the character fine but instead on one backlash, I'm getting two like so \\\ä . You can see in the code I'm using "\\\\u\u0026quot; to try and print \\u\u003c/code> , but that's not what happens. Any ideas of where I'm going wrong here?

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

The point is that you are not writing a simple text-file but a java properties-file. In a properties-file the backslash-character is an escape-character, so if your property-value contains a backslash Java is so kind to escape it for you - which is not what you want in your case.

You might try to circumvent Java's property-file-mechanism by writing a plian text-file that can be read back in as a proerties-file, but that would mean doing all the formatting that gets provided automatically by the Properties -class manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM