How do I escape # character in value field in properties file [duplicate] - java

I've got a key = value property in the .properties file:
give names: (1) code = xxx
... but when I tried to get that key, it threw an error:
No message found under code give names: (1) code = xxx
I tried escaping the whitespace with \ but it didn't work.
Do I need to escape :,(, and ) characters as well?

You could check out: http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.Reader)
For info on how java interprets a properties file. The most relevant part is:
The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator.

In my case, two leading '\\' working fine for me.
For example : if your word contains the '$' character (e.g. Rf$RF, you can escape it with two leading '\\'

Related

How to get index of Escape Character in a String?

How to get index of Escape Character in a String?
String test="1234\567890";
System.out.println("Result : "+test.lastIndexOf("\\"));
Result i get:
-1
Result i need: 4
Your original String doesn't contain \. Which means you are searching something which does not exist. Inorder to add \ to your string. You have to escape while adding
String test="1234\\567890";
System.out.println("Result : "+test.lastIndexOf("\\"));
Should work.
In your case look at the last line in the table.
I don't think you can get that because when you use an escape character is for java to interpret the following character in a special way. In another words, the escape character and the next character you see in the string are really one entity from the point of view of program being executed.
When you search for "\\", you are searching for the literal character '\' not the escape character.
Here you can see the difference: java fiddle
While \5 is the character with code 0x5 (ENQ), 5 is the character 0x35. See the table.

Regular Expression is failing in java

I have a file name which starts with %payslip%.xml.gpg.
Below are the possible file name example :
Taswkly_payslips_Pay27.xml.gpg
exec_payslip.xml.gpg
Cairns_payslips_adv_P27.xml.gpg
Could you please help me suggesting the regex for above pattern name.
In the above pattern below things are fixed i.e.
*payslip*.xml.gpg.
Any help would be appreciated.
You can use this regex:
^.*payslip.*\.xml\.gpg$
^ start of the line
.* any character multiple times
payslip the string "payslip"
.* any character multiple times
\. the "." character
xml the string "xml"
\. the "." character
gpg the string "gpg"
$ end of the line
Also don't forget to escape it in java
^.*payslip.*\\.xml\\.gpg$
Working example

Special characters in properties file

In my Java/Spring web application i had problem printing special characters of italian language (ò,à,è etc.) that I retrieve from a properties file.
I found this article http://docs.oracle.com/cd/E26180_01/Platform.94/ATGProgGuide/html/s1816convertingpropertiesfilestoescap01.html.
But something is not clear: after I run the command written in the article, in my console (CMD console of windows) i can read my properties file "translated". After it, what should i do?
Should I copy the texts from the windows console and paste them into my properties file? It doens't seem a "professional" work!
There's no need in copying the output, you may just redirect it to the file:
native2ascii notTranslated.properties > translated.properties
Also, if you're building your project with Ant, you can use native2ascii ant task, for example:
<native2ascii src="srcdir" dest="srcdir" includes="**/*._properties" ext=".properties"/>
I assume here, that the initial non-ASCII properties files are named *._properties in your project.
The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator. All of these key termination characters may be included in the key by escaping them with a preceding backslash character; for example,
\:\=
would be the two-character key ":=". Line terminator characters can be included using \r and \n escape sequences. Any white space after the key is skipped; if the first non-white space character after the key is '=' or ':', then it is ignored and any white space characters after it are also skipped. All remaining characters on the line become part of the associated element string; if there are no remaining characters, the element is the empty string "". Once the raw character sequences constituting the key and element are identified, escape processing is performed as described above.
See this link to escape special char and see this link to read by different UTF.

Storing date in .properties file java

I am trying to store a date in my config.properties file however the format is wrong.
try{
prop.setProperty("last_run_time",sdf.format(date));
prop.store(new FileOutputStream("config.properties"),null);
}
catch (Exception e){
e.printStackTrace();
}
The value of sdf.format(date)) is correct e.g. 2013-08-23 02:47 . Issue is that in the properties file 2013-08-23 02\:47 gets stored. Where does the '\' come from?
The \ unmask your :. Normaly the : is used to define a key with a value! You can read more about unmasking and the .properties file here.
This is from the Java Doc:
The key contains all of the characters in the line starting with the
first non-white space character and up to, but not including, the
first unescaped '=', ':', or white space character other than a line
terminator. All of these key termination characters may be included in
the key by escaping them with a preceding backslash character; for
example,
\:\=
would be the two-character key ":=". Line terminator characters can be
included using \r and \n escape sequences. Any white space after the
key is skipped; if the first non-white space character after the key
is '=' or ':', then it is ignored and any white space characters after
it are also skipped. All remaining characters on the line become part
of the associated element string; if there are no remaining
characters, the element is the empty string "". Once the raw character
sequences constituting the key and element are identified, escape
processing is performed as described above.
I think it is fine to save like \:
The Java property file is not a text for you to read. It is for the Java code to read. The escaping \ will ensure that the next time it is read by your Java app, it will be interpreted as a colon, not as a key/value separator.
The colon is one of the possible key/value separation characters. The leading backslash escapes it (this is only necessary when the key contains a colon, but you're more on the save side when always escaping it).
Variants of valid assignments:
key value
key= value
key: value
See Javadoc: Properties.load(Reader)

Saving to properties file escapes :

Does anyone know why the colons are getting escaped when I store the properties file?
I'm doing this:
Properties prop = new Properties();
// Set the properties value.
prop.setProperty("url","http://localhost:7101/test/home");
And storing using:
prop.store(new FileOutputStream(propFile), null);
It's working but the output has colons escaped for some reason:
url=http\://localhost\:7101/test/home
Anyone know a fix?
In properties files, both of these are legit:
key1 = value
key2: value
So both = and : must be escaped.
Now, if you read the thing back with Properties, it's no problem. Otherwise, you'll have to write custom code
That's what the store() API does:-
Each character of the key and element
strings is examined to see whether it
should be rendered as an escape
sequence. The ASCII characters \, tab,
form feed, newline, and carriage
return are written as \, \t, \f \n,
and \r, respectively. Characters less
than \u0020 and characters greater
than \u007E are written as \uxxxx for
the appropriate hexadecimal value
xxxx. For the key, all space
characters are written with a
preceding \ character. For the
element, leading space characters, but
not embedded or trailing space
characters, are written with a
preceding \ character. The key and
element characters #, !, =, and : are
written with a preceding backslash to
ensure that they are properly loaded.
It shouldn't really matter to you as long as you use Properties to get the values.

Categories

Resources