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?

Answers


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)


Need Your Help

Increase performance of WPF controls loading resources

c# .net wpf wpf-controls devexpress

I currently use DevExpress controls heavily in an application. The controls are great and speed-up development time dramatically (and hence, I wouldn't want to ditch them) however, I have a few iss...

Java api to encode URL query parameter values?

java url encode query-parameters

I used URLEncoder but this seems URLEncoder does not cover everything of url query parameter encoding (escaped). I searched and someone used URLEncoder to encode whole url. For my case I iust wan...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.