Setting property with colon (:) in it

K

Kevin Kirkeby

Any ideas why when I store a property with colons in them, they are
written as '\:' instead of ':' on Windows 2000?

Here is an example of code. When this is run, you'll see that
C:\temp\test.properties has a property set as follows:
testProp=front\:middle\:back
It should be, instead: testProp=front:middle:back

Why am I getting the extra '\'s? Is there some sort of special
character I should put before the colons?

Here is sample code:

Properties properties = new Properties();
FileInputStream fis;
fis = new FileInputStream("C:\\temp\\test.properties");
try {
properties.load(fis);
} finally {
fis.close();
}
properties.setProperty("testProp", "front:middle:back");
FileOutputStream fos = new
FileOutputStream("C:\\temp\\test.properties");
properties.store(fos,"");
fos.close();
 
C

Christophe Vanfleteren

Kevin said:
Any ideas why when I store a property with colons in them, they are
written as '\:' instead of ':' on Windows 2000?

Here is an example of code. When this is run, you'll see that
C:\temp\test.properties has a property set as follows:
testProp=front\:middle\:back
It should be, instead: testProp=front:middle:back

Why am I getting the extra '\'s? Is there some sort of special
character I should put before the colons?

Here is sample code:

Properties properties = new Properties();
FileInputStream fis;
fis = new FileInputStream("C:\\temp\\test.properties");
try {
properties.load(fis);
} finally {
fis.close();
}
properties.setProperty("testProp", "front:middle:back");
FileOutputStream fos = new
FileOutputStream("C:\\temp\\test.properties");
properties.store(fos,"");
fos.close();


Read the javadocs, that's normal behavior.
 
K

Kevin Kirkeby

I see that this is normal behavior, but is there a way to "turn off"
this behavior. I don't want the properties with the colons in them to
be written as \:.

Thanks,
Kevin
 
K

Kevin Kirkeby

I have found that using list() and directing it to a properties file
instead of using store() works better for the colon issue:
Properties props = new Properties();
PrintWriter pw = new PrintWriter(new
FileOutputStream("C:\\temp\\test.properties"));
props.list(pw);
pw.flush();
pw.close();
 
D

Dale King

I see that this is normal behavior, but is there a way to "turn off" this
behavior. I don't want the properties with the colons in them to be written
as \:.

You don't really have a choice in the matter. Properties files can use =, :,
or just whitespace to separate the key and value. If the key contains any of
these characters, the # and ! comment characters, or a backslash, then those
occurrences need to be escaped so it knows to to treat those as normal
characters and not assign their normal meaning.

I would suggest looking for an alternative to : for use in your key. It is
customary to use '-' and or '.' instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top