Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Properties: how to escape # (hash)

How do I escape the hash sign (#) in Java properties files.

We have an internationalization framework that uses Java properties files.

There is a column called number and we want its header to be #. This ...

number=#

... doesn't work.

like image 523
flybywire Avatar asked Apr 27 '13 10:04

flybywire


People also ask

How do you escape special characters in Java properties file?

I have a properties-file containing special chars in its key and value part, like #, :, ! or just a simple space. All occurences are escaped with a backslash, but IDEA marks all occurrences in the value element red.

How do I escape hash in properties file?

Since it is java, you need to replace hash( # ) with \\# in your code. Notice the double slashes. Alternately you can replace # with \\u0023 .

How delete properties file in Java?

util. Properties. clear() method is used to remove all the elements from this Properties instance. Using the clear() method only clears all the element from the Properties and does not delete the Properties.

What is in .properties file?

properties is a file extension for files mainly used in Java-related technologies to store the configurable parameters of an application. They can also be used for storing strings for Internationalization and localization; these are known as Property Resource Bundles.


1 Answers

This one should work without any escape character:

number=#

Just made a small test using code ....

prop.load(new FileInputStream ("./res/app.properties"));
System.out.println("Property: " +prop.getProperty("Number"));

... and property file (note upper/lower case):

Number=#
Text=test

Result:

Property: #

So rather check your spelling, lower/upper case or further processing.

Check out this or that one (linking Java documentation too) for more.

p.s.: though it shouldn't be required you could also try the Unicode sequence: \u0023.

Cheers!

like image 95
Trinimon Avatar answered Sep 24 '22 01:09

Trinimon