简体   繁体   English

在.properties文件Java中存储日期

[英]Storing date in .properties file java

I am trying to store a date in my config.properties file however the format is wrong. 我试图将日期存储在config.properties文件中,但是格式错误。

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 eg 2013-08-23 02:47 . sdf.format(date))值正确,例如2013-08-23 02:47 Issue is that in the properties file 2013-08-23 02\\:47 gets stored. 问题是在属性文件2013-08-23 02\\:47存储了该文件。 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 . 您可以在此处阅读有关取消屏蔽和.properties文件的更多信息。

This is from the Java Doc: 这来自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. 可以使用\\ r和\\ n转义序列包含行终止符。 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. Java属性文件不是供您阅读的文本。 It is for the Java code to read. 用于读取Java代码。 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. 转义\\将确保Java应用程序下次读取它时,会将其解释为冒号,而不是键/值分隔符。

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) 请参见Javadoc:Properties.load(Reader)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM