1

I'm trying to add hexadecimal value in a property file, the value is getting stored but i can see a "\" getting appended, which I don't want,

test.properties

#
#Fri Jun 07 21:18:49 GMT+05:30 2013
test=fe\:fe

the Java file

public class PropertySave {
    private static File s_file;
    private static Properties s_properties = new Properties();
    public static void main(String[] args) {
        loadProperties();
        s_properties.setProperty("test", "fe:fe");
        saveProperties();
    }
    private static void saveProperties() {
        FileOutputStream fout = null;
        try {
            fout = new FileOutputStream(s_file);
            s_properties.store(fout, "");
        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.exit(1);
        } finally {
            if (fout != null) {
                try {
                    fout.close();
                } catch (final IOException ioe2) {
                    ioe2.printStackTrace();
                    System.exit(1);
                }
            }
        }
    }
    private static void loadProperties() {
        s_file = new File("test.properties");
        if ((!s_file.exists()) || (!s_file.isFile())) {
            System.exit(1);
        }
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(s_file);
            s_properties.load(fin);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.exit(1);
        } finally {
            if (fin != null) {
                try {
                    fin.close();
                } catch (final IOException ioe2) {
                    ioe2.printStackTrace();
                    System.exit(1);
                }
            }
        }
    }
}

In java file the s_properties.setProperty("test", "fe:fe"); the output in property file is different(test.properties) fe:fe this i want to ignore as this property file is input to other system in "C" Language because of which my things are not working,

What Should I do to make sure the input in java file and output in properties file will same

3
  • 1
    The colon is a special character to the properties logic -- it is the separator between key and value in the properties file. So if you want a value with a colon in it, it must be stored in the properties file in "escaped" form. If you don't want this, don't use the Java properties mechanism to write the file.
    – Hot Licks
    Commented Jun 7, 2013 at 16:00
  • @HotLicks Thanks, escaped form example like ? if i give this "fe\\:fe" the output is "fe\\\:fe". my input and output must match,I cannot change the java properties mechanism for now
    – anish
    Commented Jun 7, 2013 at 16:11
  • You want too much. You're asking the properties mechanism to do something it's not designed to do.
    – Hot Licks
    Commented Jun 7, 2013 at 17:51

2 Answers 2

1

Consider encoding the hexidecimal values in a known way and decoding them in both C and Java.

One technique is to precede the hexidecimal value with "0x" and use all upper case for the digits. If you use this technique, you will need some way to signal the end of a hexidecimal number. I suggest a space character (' ') or end of line.

With this technique, your property will look like this:

test=0xFEFE

The string "Blam07kapow" where 07 is a hex number would look like this:

Blam0x07 kapow

1
  • thanks, Looks like the PropertyClass replace all the occurences of ":" with "\:" regardless of hexadecimal value, which causing problem to the extrenal system like C, is there any way for workaround
    – anish
    Commented Jun 11, 2013 at 6:41
0

The Properties class stores : with a preceding \ in order to ensure that it will load the proper value again when you use the load method. If you want to be able to load in this properties file back into java later on, then I'm afraid you are stuck with the escape character in your properties file. See java.util.Properties documentation

4
  • thanks preceding \ throws compilation error " s_properties.setProperty("test", "fe\:fe");" throws compilation error
    – anish
    Commented Jun 7, 2013 at 16:15
  • Well, if you precede the : with a \ in the actual code, then it thinks you are trying to use a Java escape character, but it likely does not have an escape sequence for \: The \: is only an escape character for storing and loading properties from a file. Commented Jun 7, 2013 at 16:20
  • If you don't absolutely need a colon in the properties (say you are reading the properties file into another programming language), then I would suggest going with DwB's solution, as most languages understand hexadecimal values in the notation 0x123DEF Commented Jun 7, 2013 at 16:24
  • Thanks, It looks like the ":" is replace by "\:" by the property class, for all value not specific to the hexadecimal, for external system, the value will always wrong is there any way to upgarde the property value there itself
    – anish
    Commented Jun 11, 2013 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.