I'm writing the FILEPATH parameter into the properties file like below..
String newFilePath = txtFilepath.getText();
Properties prop = new Properties();
java.io.File propFile = new File("src/com/app/tool/properties/settings.properties");
prop.setProperty("FILEPATH", newFilePath);
System.out.println("newFilePath "+newFilePath);
try {
prop.store(new FileOutputStream(propFile), "New File location");
} catch (IOException ex) {
ex.printStackTrace();
}
The Filepath is D:\filelog.txt
While writing, the console prints like..
newFilePath D:\filelog.txt
But when I open the properties file, content is like..
FILEPATH=D\:\\filelog.txt.
Anyone suggest me , what I'm really m(i)essing here ..?
In a properties file, : has a meaning.
key = value
key: value
both are valid. Hence, it has to be escaped while writing to a properties file. Even \ needs escaping. But when you display them on the console, the escaping is identified, and the values are shown properly.
If you see the docs of store() method of the Properties class
Every entry in this Properties table is written out, one per line. For
each entry the key string is written, then an ASCII =, then the
associated element string. 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.
Note:- It shouldn't be a problem if you use Properties to read the properties file(it will handle the escaping), but if you read the properties file like a normal text file(you should never do it unless you're out of your mind), then it'll be a problem.
Just call prop.get("FILEPATH"). You will see that you get back "D:\filelog.txt", just as you put it in.
Backslash \ is an escape character that is silently dropped in not followed by another \
Colon : is a special character that must by escaped with \.
Check docs at
http://commons.apache.org/proper/commons-configuration/userguide/howto_properties.html
Related
I have a text file which contains data. Some special char comes in the file. I need to remove all "special" characters, ie:
],à,>,¤,`,ƒ,Š,¥,Œ,^,>¤,°,ã,Ãé,–«»°,NÂ,N,º,?¿Ññ,ß,ä,º,ô5,ª,é ,ª,§,Á
These need to be replaced with a space chat, not removed.
I have one constraint that I have to store output in a String, because I need to pass that string further in TIBCO. I have written the following code but it is removing everything. As I need to have + and - symbol in file.
str = str.replaceAll("[^\\w\\s]*", "");
Any help appreciated.
Firstly, if you need to replace with whitespace and not with blank, why are you replacing with blank?
You could just use a white list of all chars you want to keep by adding plus and minus signs to the character class:
.replaceAll("[^\\w\\s.,+-]", " ")
I also added the dot and comma, since you probably want these too.
But it looks like a blanket character would be better, since all chars you don't want are above 127:
.replaceAll("[\u0080-\uffff]", " ")
You can add other chars you don't want to this character class as you need.
Note: In both cases, I removed the quantifier *, because you want a 1-for-1 replacement. If you use * the regex will match between every character, and match a sequence of unwanted chars, which will mess up your file.
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 '\\'
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.
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)
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.