Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Properties class implementation handles double/single quoted values?

I want to support property file format like below (allow quotes surround the value):

key1=value1   
key2="value2"
key3='value'

My question is does Java Properties class implementation handles double/single quoted values like above? I mean auto-removing quotes.

Actually I tried it's not, just want to confirm here. So I have to remove quotes myself.

EDIT:

I had a code below for my simple case:

String path = "/tmp/my.properties";
Properties p = new Properties();
p.load(new FileInputStream(new File(path)));

String v = p.getProperty("key2");
if((v.startsWith("\"") && v.endsWith("\"")) || 
   (v.startsWith("\'") && v.endsWith("\'"))) {
    v = v.substring(1, v.length()-1);
}

Any recommendation on best practice to handle this?

Thanks

like image 721
Eric Avatar asked Feb 05 '13 20:02

Eric


People also ask

How do you escape a single quote from properties file?

A single quote itself must be escaped by using two single quotes ('').

How do you escape a double quote in java?

Escaping double quotes in Java String If we try to add double quotes inside a String, we will get a compile-time error. To avoid this, we just need to add a backslash (/) before the double quote character. In this example, we want to print double quotes around the words “Steve” and “Java”. That's it!

How do you represent a double quote in java?

Use char(34) to represent double quotes. Java can easily represent ASCII symbols using the char type. 34 is the ASCII code for the " symbol, so write char(34) to display " without using its special meaning.

When assigning text to strings you can use single or double quotes?

They are the same thing In JavaScript, a string is a sequence of characters enclosed in single or double quotes. The choice of quoting style is up to the programmer, and either style has no special semantics over the other.


1 Answers

To remove quotes, load the property file with your own extension of ResourceBundle which overrides handleGetObject.

See also:

http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html http://docs.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html

like image 109
Dino Octavian Avatar answered Oct 17 '22 06:10

Dino Octavian